为什么这个JSON代码在Swift 3.2中不起作用

时间:2017-09-13 23:21:15

标签: json swift

我正处于应用程序第一部分的最后一段。我一直在完全按照教程,但我发现的每篇文章都有一个很好的代码。无论如何,这就是我所在的地方,我收到了两个我从未见过的错误。

这些是错误: 1.使用未解析的标识符mutableContainers 2.通话中的额外参数

任何帮助将不胜感激。我已经更改了我的URL名称以保护隐私。

 // Send user data to server side
    let myUrl = NSURL(string: 
 "https://www.nyserver.com/userRegister.php");
    let request = NSMutableURLRequest(url:myUrl! as URL);
    request.httpMethod = "POST";

    let postString = "email=\(String(describing: userEmail))&password=\(String(describing: userPassword))";

    request.httpBody = postString.data(using: String.Encoding.utf8);


    let task = URLSession.shared.dataTask(with: request as URLRequest) {
        data, response, error in

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

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

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

            var isUserRegistered:Bool = false;
            if(resultValue=="Sucess") { isUserRegistered = true; }

            var messageToDisplay:String = parseJSON["message"] as String!;
            if(!isUserRegistered)
            {
                messageToDisplay = parseJSON["message"] as String!;
            }


            DispatchQueue.main.async(execute: {

                //Display alert messagewith confirmation.
                var myAlert = UIAlertController(title:"Alert", message:messageToDisplay, preferredStyle: UIAlertControllerStyle.alert);

                let okAction = UIAlertAction(title:"Ok", style:UIAlertActionStyle.default){ action in
                    self.dismiss(animated: true, completion:nil);
                }

                myAlert.addAction(okAction);
                self.present(myAlert, animated:true, completion:nil);
            });

        }


    }

    task.resume()

0 个答案:

没有答案