调用中的额外参数错误

时间:2017-05-03 09:59:57

标签: swift2 swift2.3

我一直在尝试使用php登录一段时间,但我有一个最终的错误阻止我完成。结果是在NSJSONSerialization中给出了一个错误,错误告诉我:调用中的错误额外参数。然后我将提供一个截图,以便错误更清晰,代码使您可以帮助我,因为我处于阻塞时刻,我不知道如何解决它。提前致谢

照片错误:[ 代码:

@IBAction func loginButtonTapped(sender: AnyObject) {

    let userEmail = userEmailTextField.text;
    let userPassword = userPasswordTextField.text;



    if(userEmail!.isEmpty || userPassword!.isEmpty) { return; }

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

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

    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
        data, response, error in

        if error != nil{
            print("error=\(error)")
            return
        }

        var err: NSError?

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

        if let parseJSON = json {
            var resultValue:String = parseJSON["status"] as String!;
            print("result: \(resultValue)")

            if(resultValue=="Success")
            {
                NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isUserLoggedIn");
                NSUserDefaults.standardUserDefaults().synchronize();

                self.dismissViewControllerAnimated(true, completion: nil);
            }
        }
    }
    task.resume()
}

last error

2 个答案:

答案 0 :(得分:0)

您可以按照快速解决方法进行操作。这样做会让你这样结束:

do {
    var json = try JSONSerialization.jsonObject(with: Data(), options: .mutableContainers)
     if let parseJSON = json {
          //your code
     }
} catch {
    //handle error
}

答案 1 :(得分:0)

与Objective C不同,根据Swift中的新方法,使用try / catch块处理错误,而不是在方法中传递错误对象。

对Swift 3使用以下代码。

 do {
      var json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) 
        if let parseJSON = json {
        var resultValue:String = parseJSON["status"] as String!;
        print("result: \(resultValue)")

        if(resultValue=="Success")
        {
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isUserLoggedIn");
            NSUserDefaults.standardUserDefaults().synchronize();

            self.dismissViewControllerAnimated(true, completion: nil);
        }
    }
 } catch let error {
        //Perform the error handling here
 }

关注Swift 2

do {
      var json = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers)
        if let parseJSON = json {
        var resultValue:String = parseJSON["status"] as String!;
        print("result: \(resultValue)")

        if(resultValue=="Success")
        {
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isUserLoggedIn");
            NSUserDefaults.standardUserDefaults().synchronize();

            self.dismissViewControllerAnimated(true, completion: nil);
        }
    }
 } catch let error {
        //Perform the error handling here
 }