我使用Parse作爲移動後端到我的應用程序。我爲我的'用戶'數據表(User class)'coach'和'club'添加了2個布爾列,基本上他們是教練或俱樂部。在登錄期間需要根據這些變量的布爾值執行if語句。我的代碼目前如下:Parse.com當前用戶如果陳述
[PFUser logInWithUsernameInBackground:_usernameField.text password:_passwordField.text
block:^(PFUser *user, NSError *error) {
if (user) {
if(user.coach = @YES){
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"coach"]; //sets yes for coach value
}
if(user.club = @YES){
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"club"]; //sets yes for club
}
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"AthleteLoggedIn" bundle:nil];
UISplitViewController *new = [sb instantiateInitialViewController];
self.view.window.rootViewController = new;
} else {
NSString *errorString = [error userInfo][@"error"];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Oops" message:errorString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}];
'如果(user.coach = @YES)'應是'if(user.coach == @YES)'。 'user.club'也一樣。在你的陳述中,你給變量賦了'@ YES',然後評估變量的值(當然是'@ YES') – MByD 2014-09-22 09:24:36
爲了避免下一次你應該養成把常量放在左邊的習慣比較if(@YES == user.coach)。如果(@YES = user.couch)會引發語法錯誤。 – ardrian 2014-09-22 11:36:58