【问题标题】:Does converting this Objective-C code from a Parse tutorial into Swift equate to the same check将此 Objective-C 代码从 Parse 教程转换为 Swift 是否等同于相同的检查
【发布时间】:2015-01-01 00:33:17
【问题描述】:

我一直在应用程序中使用 Parse,在使用他们预先制作的登录和注册视图的教程中,有一个检查是否应该根据用户名和密码字段启动登录过程。在objective-c中,代码是这样的

// Sent to the delegate to determine whether the log in request should be submitted to the server.
- (BOOL)logInViewController:(PFLogInViewController *)logInController shouldBeginLogInWithUsername:(NSString *)username password:(NSString *)password {
    // Check if both fields are completed
    if (username && password && username.length != 0 && password.length != 0) {
        return YES; // Begin login process
}
[[[UIAlertView alloc] initWithTitle:@"Missing Information"
                      message:@"Make sure you fill out all of the information!"
                      delegate:nil
                      cancelButtonTitle:@"ok"
                      otherButtonTitles:nil] show];
    return NO; // Interrupt login process
}

我关心的主要部分是 if 语句 if (username && password && username.length != 0 && password.length != 0) 据我所知,这不会直接转换为 swift。我可以使它工作的最好的方法是检查与他们的逻辑相反的逻辑,如下面的 Swift 所示

  func logInViewController(logInController: PFLogInViewController!, shouldBeginLogInWithUsername username: String!, password: String!) -> Bool
    {
      // Check to make sure both fields are filled out
      if(username.isEmpty | password.isEmpty)
      {
        let alertView: UIAlertView = UIAlertView(title: "Missing Information", message: "Please make sure all fields are filled out!", delegate: nil, cancelButtonTitle: "ok")
        alertView.show()

        return false
      }

      return true
  }

如果有人已经直接将此函数转换为 Swift,我找不到任何东西。不过,我主要关心的是支票是否相等。我觉得我的 Swift 转换可能不会涵盖与 Obj-C 相同的检查。我真的很感激一些帮助,看看它是否真的检查一样。

编辑: 主要问题是您无法使用 Swift 中的 obj-c 之类的字符串检查是否相等。它说字符串不能用作布尔值。但即使有类型转换,我也不确定它是否会起作用。

【问题讨论】:

    标签: ios objective-c swift parse-platform


    【解决方案1】:
        if usrEntered != "" && pwdEntered != "" && emlEntered != "" {
            //insert code here for successfull login
        }
    

    这应该是您正在寻找的。

    【讨论】:

    • 这不起作用,因为您正在尝试比较一个字符串,它期望一个布尔值。我在上面的解释中说过。
    • 为什么不在运行登录功能之前插入支票?这样你就不会遇到这个问题了
    • 因为我正在使用特定的 API,并且委托协议需要上面的代码
    【解决方案2】:
    if(countElements(username) != 0 && countElements(password) != 0){
        return true;
    }
    

    这应该是你要找的。​​p>

    【讨论】:

    • 我用 String.utf16count 尝试过这个(类似的东西),但我仍然遇到同样的错误,我知道 countElements 以类似的方式工作。你测试过吗?
    • 对此进行了测试,不起作用。说它不是已知的二元运算符。我相信我的解决方案可能是迄今为止唯一真正有效的解决方案。不过,我很想看看其他人的工作版本
    • 不,它不起作用,它会引发两个错误,但它本质上是一回事。看来你不能像布尔一样比较 Swift 中的字符串。我相信 .isEmpty 是检查空字符串的唯一方法
    【解决方案3】:
    func logInViewController(logInController: PFLogInViewController!, shouldBeginLogInWithUsername username: String!, password: String!) -> Bool {
        if (username != nil && password != nil && !username.isEmpty && !password.isEmpty) {
            return true
        }
        let alertView = UIAlertView(title: "Missing Information", message: "Make sure you fill out all of the information!", delegate: nil, cancelButtonTitle: "OK")
        alertView.show()
        return false
    }
    

    这是您的直接翻译。您在 Objective-C 中缺少的检查是“用户名!= nil && 密码!= nil”。但是,在我的 Xcode 版本中,参数不是可选参数,因此该代码无法编译。您实际上可以取出检查 nil 因为用户名和密码参数不是类型“字符串?” (可选),因此他们保证在那里。

    【讨论】:

      猜你喜欢
      • 2017-12-17
      • 1970-01-01
      • 2018-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-04
      • 1970-01-01
      相关资源
      最近更新 更多