Json 预期的'';分隔符和预期值(#x27;)';在表达式列表中
我收到以下脚本中的两个错误:Json 预期的'';分隔符和预期值(#x27;)';在表达式列表中,json,swift,nserror,Json,Swift,Nserror,我收到以下脚本中的两个错误: request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in if error != nil {
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
data, response, error in
if error != nil {
print("error=\(error)")
return
}
do {
if let parseJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
print(parseJSON)
let resultValue:String = parseJSON["status"] as! String
print("Result: \(resultValue)")
print(userEmail)
print(userPassword)
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(), {
//Display alert message with confirmation
let myAlert = UIAlertController(title: "Alert", message:messageToDisplay, preferredStyle: UIAlertControllerStyle.Alert);
let okAction = UIAlertAction(title:"Alert", style:UIAlertActionStyle.Default){
action in self.dismissViewControllerAnimated(true, completion:nil);
}
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated:true, completion:nil);
)};
}
}
catch let error as NSError {
print(error.localizedDescription)
}
}
task.resume()
}
}
第一个错误是:Expected',“separator
在这一行:
)};
它的意思是插入“,”,但这是一个持续不断的错误,并没有结束
在下面的下一行,也就是一个括号中,我得到了一个错误:Expected'),在表达式列表中(not)}
您想要的是}
,而不是)}
(或者可以使用尾随闭包语法)。还要注意的是,在行尾不需要分号,在if
条件周围也不需要括号。Swift不是脚本语言(与PHP不同);-)
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
data, response, error in
if error != nil {
print("error=\(error)")
return
}
do {
if let parseJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
print(parseJSON)
let resultValue:String = parseJSON["status"] as! String
print("Result: \(resultValue)")
print(userEmail)
print(userPassword)
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(), {
//Display alert message with confirmation
let myAlert = UIAlertController(title: "Alert", message:messageToDisplay, preferredStyle: UIAlertControllerStyle.Alert);
let okAction = UIAlertAction(title:"Alert", style:UIAlertActionStyle.Default){
action in self.dismissViewControllerAnimated(true, completion:nil);
}
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated:true, completion:nil);
});
}
}
catch let error as NSError {
print(error.localizedDescription)
}
}
task.resume()
}
}