2015-04-12 32 views
1

我將Xcode更新爲6.3,並發現我的代碼中存在一些新的Swift 1.2新錯誤。Swift 1.2中令我困惑的更改

user.signUpInBackgroundWithBlock { 
    (success:Bool!, error:NSError!) -> Void in 

    if !(error != nil) { 
     println("sign up successfully") 

     var loginAlert: UIAlertController = UIAlertController(title: "Sign Up", message: "Sign Up Succeeded", preferredStyle: UIAlertControllerStyle.Alert) 
     self.presentViewController(loginAlert, animated: true, completion: nil) 
     loginAlert.addAction(UIAlertAction(title: "Okay", style: 

我得到這個錯誤:

Cannot invoke signUpInBackgroundWithBlock with an argument list of type ((Bool!, NSError!) -> void)

我怎樣才能解決這個問題?

另外一個

@IBAction func endend(sender: AnyObject) {   
    if (PFUser.currentUser() == nil) { 
     PFUser.logInWithUsernameInBackground(usernameTextField.text, password: passwordTextField.text){ 
      (user:PFUser!, error:NSError!) -> Void in 

      if user != nil { 
       println("login chenggong") 
       var tlvc = TimelineViewControllerTableViewController()     
       self.presentViewController(tlvc, animated: true, completion: nil) 
      } 
      else { 
       println("failed") 
      }   
     }  
    } 
} 

我得到這個錯誤:

「UITextField」 does not have member named 「text」.

而且我得到了3個錯誤,大約},它說

Expected 「,」 separator.

Expected expression in list of expressions.

Expected 「)」 in expressions.

我可以跑到我的應用程序斯威夫特以前1.2,但現在...

+1

'如果(誤差=零!)'會更理智地寫成'如果錯誤== nil'你能否也向我們展示了'signUpInBackgroundWithBlock'方法的宣言!? – Sulthan

+0

你在用戶名後缺少冒號 – Sulthan

+0

我找不到你剛剛提到的用戶名。但感謝您的幫助! –

回答

0

在Xcode中,轉到編輯>轉換...>到最新的Swift語法...

新版本中有幾種語言語法更改,所以Apple包含一個工具來幫助遷移較舊的Swift代碼。從我一直在閱讀它很有用,但並不總是解決100%的問題。希望它至少能減少你看到的錯誤數量。

+0

謝謝,但我得到更多的錯誤大聲笑 –

0

對於解析註冊塊,當您強制解包布爾成功參數時,Swift 1.2編譯器不喜歡它。

刪除'!' '成功後:布爾'應該刪除你正在得到的錯誤。

嘗試改變:

user.signUpInBackgroundWithBlock{(success:Bool!, error:NSError!) -> Void in 

要:

user.signUpInBackgroundWithBlock{(success:Bool, error:NSError!) -> Void in 
+0

它不起作用,但仍然感謝您的幫助。 –

1

以下代碼爲我工作:

PFUser.logInWithUsernameInBackground(username.text as String!, password: password.text as String!){ 
       (loggedInuser: PFUser?, signupError: NSError?) -> Void in 
0

嘗試改變:

user.signUpInBackgroundWithBlock{(success:Bool!, error:NSError!) -> Void in 

要:

user.signUpInBackgroundWithBlock{(success:Bool?, error:NSError?) -> Void in