我已经阅读了一些内容,并且在类似的帖子(使用 Alamofire 的 Swift 闭包)中提出了建议,并尝试对我的代码执行相同的操作,但我现在找不到调用该函数的方法?
我收到以下错误:无法将表达式的类型“(响应:@lvalue String)”转换为类型“((响应:字符串)->())->()”
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
}
}
谢谢!
科斯塔斯