我正在尝试使用NSMutableURLRequest来使用API REST,并解析json我正在使用SwiftyJSON,Consume Rest API,我的代码是
func getUserLogin(username:String,password:String, onSuccess:@escaping(JSON)-> Void, onFailure: @escaping(Error)->Void){
let url:String = baseURL+APIManager.getUserLogin
let request: NSMutableURLRequest = NSMutableURLRequest(url: NSURL(string: url)! as URL)
request.httpMethod="POST"
let postString = "username="+username+"&password="+password
request.httpBody = postString.data(using: .utf8)
let session = URLSession.shared
let task = session.dataTask(with: request as URLRequest,completionHandler:{data,response,error->Void in if(error != nil){onFailure(error!)
}else {
let result = JSON(data: data!)
onSuccess(result)
}
})
task.resume()
}
但是xcode构建器告诉我这部分有错误
let result = JSON(data: data!)
onSuccess(result)
任何想法为什么?因为当我尝试从Consume Rest API运行示例项目时,一切都运行正常