我有一个Seperated Swift文件用于通话。这看起来像这样:
var DEVICEBUNDLE_API_ROOT = "https://apps-dev.profects.nl/profects-apps/current/web/app.php/api/v2/device"
var session = NSURLSession.sharedSession()
init(action: NSString, data: NSString, callback: (success: Bool, data: NSDictionary) -> Void) {
var body = "{\"action\":\"" + action + "\",\"data\":" + data + "}";
var request = NSMutableURLRequest(URL: NSURL(string: DEVICEBUNDLE_API_ROOT)!)
request.HTTPMethod = "POST"
request.HTTPBody = body.dataUsingEncoding(NSUTF8StringEncoding) // Use UTF-8
var task = session.dataTaskWithRequest(request, completionHandler: {
data, response, error -> Void in
var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Response: \(strData)")
var json = NSJSONSerialization.JSONObjectWithData(data, options : .MutableLeaves, error: nil) as? NSDictionary
if let parsedJSON = json
{
if let status = parsedJSON["status"] as? NSDictionary
{
if let statusCode = status["code"] as? NSString
{
if let responseData = parsedJSON["data"] as? NSDictionary
{
callback(success: statusCode == "201", data: responseData)
}
else
{
callback(success: false, data: NSDictionary())
}
}
}
}
})
task.resume()
}
登录呼叫如下:
var request = JSONRequest("registerDeviceId", "{\"email\":\"" + usernameField.text + "\",\"password\":\"" + passwordField.text + "\", \"UUID\":\"Tset124235346456457567\", \"OS\":\"Android\"}")
现在出现错误,显示'缺少参数'回调'在电话'
我该如何解决这个问题?我已经尝试过添加一个值""在通话结束时。
答案 0 :(得分:0)
你错过了回调。这是在请求完成运行时将调用的闭包。
let action = "registerDeviceId"
let data = ...whatever...
var request = JSONRequest(action: action, data: data, callback: { (success: Bool, data: NSDictionary) in
// Do something here with the success and data info that you got from the request
return
})
有关closures
的更多信息