我正在尝试制作一个代码,但事实证明它给了我一个错误,我无法解决它而且我已经封锁了自己。我需要帮助,然后我留下一个错误和代码。所有帮助将不胜感激
代码:
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()
答案 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);
});
}