在调用中更改Swift 2 Extra参数'error'

时间:2015-09-16 22:46:55

标签: swift2

升级到Xcode 7 Swift 2和SDK for iOS 9.我收到错误“额外参数”错误“在电话中”我的代码是:

let myUrl = NSURL(string: "http://localhost/SwiftAppAndMySQL/scripts/registerUser.php");
        let request = NSMutableURLRequest(URL:myUrl!);
        request.HTTPMethod = "POST";

        let postString = "userEmail=\(userEmail)&userFirstName=\(userFirstName)&userLastName=\(userLastName)&userPassword=\(userPassword)";

        request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

        NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data:NSData!, response:NSURLResponse!, error:NSError!) -> Void in

            dispatch_async(dispatch_get_main_queue())
            {                    
                spinningActivity.hide(true)

                if error != nil {
                    self.displayAlertMessage(error.localizedDescription)
                    return
                }

                var err: NSError?
                var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: &err) as? NSDictionary

                if let parseJSON = json {

                    var userId = parseJSON["userId"] as? String

                    if( userId != nil)
                    {
                        var myAlert = UIAlertController(title: "Alert", message: "Registration successful", 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)
                    } else {
                        let errorMessage = parseJSON["message"] as? String
                        if(errorMessage != nil)
                        {
                            self.displayAlertMessage(errorMessage!)
                        }    
                    }                        
                }             
            }

        }).resume()

2 个答案:

答案 0 :(得分:1)

NSURLSession.sharedSession()。dataTaskWithRequest(request, completionHandler )会询问您3个选项参数,并且您将给3个强制解包参数。

尝试更改

NSURLSession.sharedSession().dataTaskWithRequest(request) { (data:NSData!, response:NSURLResponse!, error:NSError!)

NSURLSession.sharedSession().dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?)

答案 1 :(得分:1)

现在正在工作,我用这个::

替换了以前的代码

让myUrl = NSURL(字符串:“http://dcapp1.testingview.com/DryCleanAppClientes/scripts/registerUser.php”);         let request = NSMutableURLRequest(URL:myUrl!);         request.HTTPMethod =“POST”;

    let postString = "userEmail=\(userEmail)&userFirstName=\(userFirstName)&userLastName=\(userLastName)&userPassword=\(userPassword)";

    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

    print(postString)

    NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in

        dispatch_async(dispatch_get_main_queue())
            {

                spinningActivity.hide(true)

                if error != nil {
                    self.displayAlertMessage(error!.localizedDescription)
                    return
                }


                do {
                    let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary

                    if let parseJSON = json {

                        let userId = parseJSON["userId"] as? String

                        if( userId != nil)
                        {
                            let myAlert = UIAlertController(title: "Mensaje", message: "¡Registro exitoso!", 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)
                        } else {
                            let errorMessage = parseJSON["message"] as? String
                            if(errorMessage != nil)
                            {
                                self.displayAlertMessage(errorMessage!)
                            }

                        }

                    }

                } catch _ as NSError {



                }

        }

    }).resume()

}