我已经做了一些阅读,并在类似的帖子(Swift closure with Alamofire)有建议,并试图对我的代码做同样的事情,但我无法找到现在调用该函数的方式?快速函数完成
我得到的错误:无法转换表达式的类型“(回应:@lvalue字符串)为键入 '((回应:字符串) - >()) - >()'
import UIKit
class myClass101: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
var api_error: String = ""
activityInd.startAnimating()
call_api_function(response: api_error)
activityInd.stopAnimating()
if (api_error != "") {
let alertController = UIAlertController(title: "Server Alert", message: "Could not connect to API!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
}
}
功能如下:
func call_api_function(completion: (response: String) ->()) {
let api_url_path: String = "http://\(str_api_server_ip):\(str_api_server_port)"
let api_url = NSURL(string: api_url_path + "/devices.xml")!
Alamofire.request(.GET, api_url)
.authenticate(user: str_api_username, password: str_api_password)
.validate(statusCode: 200..<300)
.response { (request, response, data, error) in
var senderror: String = error!.localizedDescription
completion(response: senderror)
if (error != nil) {
println(senderror)
}
// do other tasks here
}
}
谢谢!
科斯塔斯·
非常感谢您的输入Rob!非常感激!!!! – Kostas 2014-11-23 17:44:30
顺便说一句,[您的其他问题的答案](http://stackoverflow.com/a/27089820/1271826)显示了一个具有两个参数的闭包示例。很明显,考虑到你正在处理XML,你可能也想解析它,但是另一个答案说明了通过闭包传递两个参数的想法。 – Rob 2014-11-23 23:06:11