2014-02-27 44 views
2

我是Xcode的初學者。在用戶在文本框中輸入登錄名和密碼後,我想獲得usernameID。我確信我做錯了。 我的代碼錯誤是在授予訪問權之後。用coredata謂詞驗證後獲取用戶名

應用程序由於未捕獲的異常

'`NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: 
                 object cannot be nil`' 

這裏是我的代碼:

- (IBAction)stepInButton:(id)sender { 

AppDelegate *appDelegateCoreData = [[UIApplication sharedApplication] delegate]; 
NSManagedObjectContext *context = [appDelegateCoreData managedObjectContext]; 
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Player" inManagedObjectContext:context]; 
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
[request setEntity:entityDesc]; 
NSPredicate *pred = [NSPredicate predicateWithFormat: 
      @"(playerlogin = %@) AND (playerpassword =%@)", self.loginTextField.text, self.passwordTextField.text]; 

[request setPredicate:pred]; 
NSError *error; 
NSArray *objects = [context executeFetchRequest:request error:&error]; 

if ([objects count] == 0) { 
    self.loginStatusLabel.hidden = NO; 
    NSLog(@"LOGIN IN ACCESS DENIED"); 

} else { 
    NSLog(@"LOGIN IN ACCESS GRANTED"); 


    NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"playerfirstname"]; 
    NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; 
    [expressionDescription setExpression:keyPathExpression]; 
    [expressionDescription setExpressionResultType:NSDateAttributeType]; 
    [request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]]; 
    NSError *error; 
    NSArray *objects = [context executeFetchRequest:request error:&error]; 

    if ([objects count] == 0) { 

     // Handle the error. 

    } 

    else { 

     if ([objects count] > 0) { 

      NSString *playerFirstName = [[objects objectAtIndex:0] valueForKey:@"playerfirstname"]; 
      NSLog(@"PlayerFirstName: %@", [[objects objectAtIndex:0] valueForKey:@"playerfirstname"]); 
      self.welcomePlayerLabel.text = [NSString stringWithFormat: @"Welcome %@", playerFirstName]; 
     } 

    } 



    self.loginStatusLabel.hidden = YES; 
    self.signButton.hidden = YES; 
    self.stepButton.hidden =YES; 
    self.loginStatusLabel.text =(@"Access Granted"); 
    self.loginStatusLabel.textColor = [UIColor greenColor]; 
    self.loginStatusLabel.hidden = NO; 

    //FirstViewController *firstViewController = [[FirstViewController alloc] init]; 
    //[self.navigationController pushViewController:firstViewController animated:YES]; 
    //[self.navigationController pushViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"first"] animated:YES]; 
} 
} 
+1

是'playerfirstname'(應重命名爲'playerFirstName')一個'NSDateAttributeType'(未'NSStringAttributeType')?你也可以簡單地在要獲取的屬性中指定名稱,並且爲了使請求按預期工作,應該將結果類型更改爲字典。爲什麼不簡單地從第一個查詢中返回的對象中獲取名字? –

+0

我沒有看到任何試圖將對象插入到NSMutableArray中,無論是顯式還是隱式。所以,也許崩潰是在代碼的不同部分,而不是你分享的部分。你能設置一個異常斷點(調試>>斷點>>創建異常斷點)並找到究竟哪一行崩潰並與我們分享? –

+1

感謝您的回覆。我嘗試了NSStringAttributeType,但沒有工作。我想像你說的那樣直接從objet獲取名字。但我不知道如何。我知道對象數組包含所有玩家信息的1條記錄。你能幫我麼 ?謝謝對於瑞安,我認爲它的這一行 - > [請求setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];也謝謝你 。 –

回答

2

確定我得到了它。

我像丹謝利說的那樣直接從playerFirstName對象中獲取對象。謝謝你丹。

- (IBAction)stepInButton:(id)sender { 

AppDelegate *appDelegateCoreData = [[UIApplication sharedApplication] delegate]; 
NSManagedObjectContext *context = [appDelegateCoreData managedObjectContext]; 
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Player"  inManagedObjectContext:context]; 
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
[request setEntity:entityDesc]; 
NSPredicate *pred = [NSPredicate predicateWithFormat: 
     @"(playerlogin = %@) AND (playerpassword =%@)", self.loginTextField.text, self.passwordTextField.text]; 

[request setPredicate:pred]; 
NSError *error; 
NSArray *objects = [context executeFetchRequest:request error:&error]; 

if ([objects count] == 0) { 
self.loginStatusLabel.hidden = NO; 
NSLog(@"LOGIN IN ACCESS DENIED"); 

} else { 
NSLog(@"LOGIN IN ACCESS GRANTED"); 
    NSString *playerFirstName = [[objects objectAtIndex:0] valueForKey:@"playerfirstname"]; 

    self.welcomePlayerLabel.text = [NSString stringWithFormat: @"Welcome %@", playerFirstName]; 
    self.loginStatusLabel.hidden = YES; 
    self.signButton.hidden = YES; 
    self.stepButton.hidden =YES; 
    self.loginStatusLabel.text =(@"Access Granted"); 
    self.loginStatusLabel.textColor = [UIColor greenColor]; 
    self.loginStatusLabel.hidden = NO; 

    } 
}