【发布时间】: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