Swift中的回调函数语法 [英] Callback function syntax in Swift

查看:680
本文介绍了Swift中的回调函数语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的代码:

  func showStandardPrompt(提示:String,view:UIViewController,numberInput:Bool,callback:(() - >(String))?){
let alert = UIAlertController(title:Input Data,message:prompt,preferredStyle:.Alert)
alert.addTextFieldWithConfigurationHandler {(textField)in
if numberInput {
textField.keyboardType = .NumberPad
}
}


中让OKAction = UIAlertAction(title:OK,style:.Default){(action)let field = alert .textFields![0] as UITextField
callback?(field.text!)
}

alert.addAction(OKAction)
let CancelAction = UIAlertAction(title: 取消,样式:.Default,处理程序:无)
alert.addAction(CancelAction)
view.presentViewController(alert,animated:true,完成:无)
}

我得到的错误是

  callback?(field.text!)

错误是无法将'String'的值类型转换为期望的参数类型'()'。我知道这是一个语法问题 - 只是不知道它应该是什么。

解决方案

Rob的回答是正确的,尽管我我想分享一个简单的工作回调/完成处理程序的例子,你可以在下面下载一个示例项目,并试验 getBoolValue 的输入。



下载示例项目



  func getBoolValue(number:Int,completion:(result:Bool) - >()){
if号码> 5 {
completion(result:true)
} else {
completion(result:false)
}
}

getBoolValue(8 ){(结果) - > ()in
//做结果
print(result)
}

重要理解:

 (String) - >()/ /需要一个字符串返回void 
() - >(String)//获取void返回一个字符串


I am attempting pass a function to another function and then have the passed function executed passing to it a variable.

Here is my code:

func showStandardPrompt(prompt:String,view: UIViewController,numberInput: Bool, callback: (()->(String))?) {
    let alert = UIAlertController(title: "Input Data", message: prompt, preferredStyle: .Alert)
    alert.addTextFieldWithConfigurationHandler { (textField) in
        if numberInput {
            textField.keyboardType = .NumberPad
        }
    }

    let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
        let field = alert.textFields![0] as UITextField
        callback?(field.text!)
    }

    alert.addAction(OKAction)
    let CancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
    alert.addAction(CancelAction)
    view.presentViewController(alert,animated: true, completion: nil)
}

The error I get is in

callback?(field.text!)

The error is "Cannot convert value type of 'String' to expected argument type '()'. I know it's a matter of syntax - just don't know what it should be.

解决方案

Rob's answer is correct, though I'd like to share an example of a simple working callback / completion handler, you can download an example project below and experiment with the getBoolValue's input.

download example project

func getBoolValue(number : Int, completion: (result: Bool)->()) {
    if number > 5 {
        completion(result: true)
    } else {
        completion(result: false)
    }
}

getBoolValue(8) { (result) -> () in
    // do stuff with the result
    print(result)
}

Important to understand:

(String)->() // takes a String returns void
()->(String) // takes void returns a String

这篇关于Swift中的回调函数语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆