调用NSJSONSerialization中的额外参数错误

时间:2017-05-02 10:40:18

标签: php swift swift3

我正在尝试制作一个代码,但事实证明它给了我一个错误,我无法解决它而且我已经封锁了自己。我需要帮助,然后我留下一个错误和代码。所有帮助将不胜感激

enter image description here

代码:

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: &err) as? NSDictionary

 if let parseJson = json{
    var resultValue = parseJson["status"] as? String
    println("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(), {
        var myAlert = UIAlertController(title: "Alert", message: messageToDisplay, 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()

1 个答案:

答案 0 :(得分:0)

试一试:

var pasedJson : [String:AnyObject]? {
if let parsedData = try? JSONSerialization.jsonObject(with: data) as? [String:AnyObject] {
    print("Json :\(parsedData ?? [String:AnyObject]())")
                var resultValue = parseJson["status"] as? String
        println("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(), {
            var myAlert = UIAlertController(title: "Alert", message: messageToDisplay, 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);

        });
}