-4

I have been trying to login for a while using php but I have a final bug that stops me from finishing. It turns out to give a bug in NSJSONSerialization where the error tells me: error extra argument in call. Then I will provide a screenshot so that the error is clearer and the code so you can help me since I am in a blocking moment and I do not know how to solve it. Thanks in advance

Photo error: [ Code:

@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

stack amr
  • 1
  • 3
  • https://developer.apple.com/reference/foundation/jsonserialization/1415493-jsonobject - drop the `NS`, change the method name, remove the `error`, wrap it in a `try`. – luk2302 May 03 '17 at 10:03
  • Can you adapt it with my code please? – stack amr May 03 '17 at 10:10
  • If it works, but in the line of if let parseJSON = json I have an error that says: use of unresolved identifier 'json' – stack amr May 03 '17 at 10:21

2 Answers2

0

You can follow the quick fix sugestions. Doing so will let you end up like this:

do {
    var json = try JSONSerialization.jsonObject(with: Data(), options: .mutableContainers)
     if let parseJSON = json {
          //your code
     }
} catch {
    //handle error
}
shallowThought
  • 19,212
  • 9
  • 65
  • 112
0

Unlike Objective C, as per the new methodolgy in Swift, errors are handled using try/catch block instead of passing the error object in the method.

Use the following code for 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
 }

Following for 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
 }
Tafveez Mehdi
  • 456
  • 3
  • 12