【问题标题】:Changes in Swift 1.2 that confusing me让我困惑的 Swift 1.2 的变化
【发布时间】:2015-06-17 22:42:46
【问题描述】:

我将 Xcode 更新到 6.3,发现新的 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:

我收到了这个错误:

无法使用类型为 ((Bool!, NSError!) -> void) 的参数列表调用 signUpInBackgroundWithBlock

我该如何解决?

另一个

@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”没有名为“text”的成员。

我收到了 3 个关于 } 的错误,上面写着

预期的“,”分隔符。

表达式列表中的预期表达式。

表达式中应为“)”。

我可以在 Swift 1.2 之前运行我的应用,但现在...

【问题讨论】:

  • if !(error != nil) 写成if error == nil 更合理,你能告诉我们signUpInBackgroundWithBlock 方法的声明吗?
  • 用户名后面缺少冒号
  • 我找不到你刚才提到的用户名。但是感谢您的帮助!

标签: swift objective-c-nullability


【解决方案1】:

在 Xcode 中,转到 Edit > Convert... > To Latest Swift Syntax...

新版本中有几处语言语法更改,因此 Apple 提供了一个工具来帮助迁移旧的 Swift 代码。从我一直在阅读的内容来看,它在很大程度上很有用,但并不总是能解决 100% 的问题。希望它至少会减少您看到的错误数量。

【讨论】:

    【解决方案2】:

    对于 Parse 注册块,当您强制解开布尔成功参数时,Swift 1.2 编译器不喜欢它。

    删除“!”在 'success:Bool' 之后应该删除你得到的错误。

    尝试改变:

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

    收件人:

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

    【讨论】:

      【解决方案3】:

      以下代码对我有用:

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

      【讨论】:

        【解决方案4】:

        尝试改变:

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

        收件人:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多