2014-09-22 25 views
0

我使用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]; 
            } 
           }]; 
+2

'如果(user.coach = @YES)'應是'if(user.coach == @YES)'。 'user.club'也一樣。在你的陳述中,你給變量賦了'@ YES',然後評估變量的值(當然是'@ YES') – MByD 2014-09-22 09:24:36

+0

爲了避免下一次你應該養成把常量放在左邊的習慣比較if(@YES == user.coach)。如果(@YES = user.couch)會引發語法錯誤。 – ardrian 2014-09-22 11:36:58

回答

0

您的代碼有兩個問題。首先是您正在使用賦值運算符=,第二個是您使用點符號表示Parse對象。

你有什麼:

if(user.coach = @YES){ /* ... */ } 
if(user.club = @YES){ /* ... */ } 

的正確實施:

if([user objectForKey:@"coach"] == @YES){ /* ... */ } 
if([user objectForKey:@"club"] == @YES){ /* ... */ } 

這可以簡化爲:

if([user objectForKey:@"coach"]){ /* ... */ } 
if([user objectForKey:@"club"]){ /* ... */ } 
+1

謝謝你,因爲你可能猜到我對Xcode很陌生,但我真的很確定應該得到double =。儘管非常感謝,他的減少對我的理解非常有幫助。通過閱讀Parse的文檔,我不確定他們在變量中聲明瞭哪些語句。這非常清楚。再次感謝大家! – Kaconym 2014-09-22 17:38:29