预计“”分隔符和表达式列表预期‘)’

问题描述:

我收到两个错误从下面的脚本:预计“”分隔符和表达式列表预期‘)’

 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 ',' seperator在这条线:

)}; 

它对insert ","说,但它是一个连续的错误,并没有结束。

在下面的下一行,这仅仅是一个支架},我得到的错误:Expected ')' in the expression list

+1

你想'})',而不是')}' (或者你可以使用尾随闭包语法)。另外请注意,您不需要在行尾添加分号,也不需要围绕“if”条件使用括号。 – Hamish

+1

Swift是**不** ** *脚本*语言(不像PHP);-) – vadian

})不)}

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() 

} 

}