我收到标题中描述的错误。
Invalid conversion from throwing function of type '(_, _, _) throws -> ()' to non-throwing function type '(NSData?, NSURLResponse?, NSError?) -> Void'
在 session dataTaskWithRequest 之后。这是一个简单的用户注册到 PHP 和 mysql。在 Swift xcode 7.3 中。
func displaymyalertmessage(userMessage:String)
{
var myAlert = UIAlertController(title: "Alert" , message:userMessage, preferredStyle: UIAlertControllerStyle.Alert);
let OkAction = UIAlertAction (title: "Ok", style: UIAlertActionStyle.Default, handler:nil);
myAlert.addAction(OkAction);
self.presentViewController(myAlert, animated:true, completion:nil);
}
// check for empty fields
if(useremail!.isEmpty || username!.isEmpty ||
userpassword!.isEmpty ||
userrepeatpassword!.isEmpty){
// Display alert message
displaymyalertmessage("All fields are required");
return;
}
//Check if passwords match
if(userpassword != userrepeatpassword)
{
// display an alert message
displaymyalertmessage("Passwords do not match");
return;
}
// send data to server
let myUrl = NSURL(string: "http://example/xxx.php")
let request = NSMutableURLRequest(URL:myUrl!);
var session = NSURLSession.sharedSession()
request.HTTPMethod = "POST";
let postString = "email=\(useremail)&password=\(userpassword)&username=\(username)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
在下一个任务中我收到错误 (_, _, _)
do {
let task = session.dataTaskWithRequest(request)
{ (data, response, error) in
// if error
if error != nil {
print("error=\(error)")
return
}
var err: NSError?
var json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers, error: &err) as? NSDictionary
if let parseJSON = json {
var resultvalue = parseJSON["status"] as? String
printld("result: \(resultvalue)")
var isuserregistered:Bool = false;
if(resultvalue=="Success") { isuserregistered = true; }
var messagetodisplay:String = parseJSON["message"] as String!;
if(!isuserregistered)
{
messagetodisplay = parseJSON["message"] as String!;
}
dispatch_async(dispatch_get_main_queue(), {
// Display alert message with confirmation
var myAlert = UIAlertController(title: "Alert" , message: "Registration is successful. Thank You", preferredStyle: UIAlertControllerStyle.Alert);
let OkAction = UIAlertAction (title: "Ok", style: UIAlertActionStyle.Default){ ACTION in
self.dismissViewControllerAnimated(true, completion: nil);
}
myAlert.addAction(OkAction);
self.presentViewController(myAlert, animated:true, completion:nil)
});
} }
task.resume()
}
我已经尝试了很多东西,但我无法清除这个错误。
最佳答案
您需要将 try-catch
移动到回调 block 作用域中。此外,您不应通过引用将 err
传递给 JSONObjectWithData
,因为它会引发错误。这应该有效:
let task = session.dataTaskWithRequest(request) { (data, response, error) in
// if error
if error != nil {
print("error=\(error)")
return
}
var json: NSDictionary? = nil
do {
json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
} catch {
print("error=\(error)")
}
//...
关于php - 无法清除错误从类型 '(_, _, _) throws -> ()' 的抛出函数到非抛出函数类型的无效转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37591510/