使用核心数据谓词在身份验证后获取用户名

时间:2014-02-27 04:44:54

标签: ios objective-c core-data

我是Xcode的初学者。用户在文本字段中输入登录名和密码后,我想获得usernameID。我确信我做错了。 我的代码错误是在授予访问权限之后。

应用程序由于未捕获的异常

 '`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 个答案:

答案 0 :(得分:2)

好的,我明白了。

我直接将玩家的名字直接写成了丹·雪莉说的对象。谢谢Dan.

- (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;

    }
}