ios - 使用核心数据谓词进行身份验证后获取用户名

标签 ios objective-c core-data

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

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

 '`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];
}
}

最佳答案

好的,我明白了。

正如 Dan Shelly 所说,我直接从对象收到了玩家名字。谢谢丹。

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

    }
}

关于ios - 使用核心数据谓词进行身份验证后获取用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22059217/

相关文章:

iphone - 在实例变量中保存 block

ios - 书法绘图 IOS Coregraphics

ios - 将几个互为 sibling 的 VC 添加到父 VC

ios - 如何删除 map 路线的这条折线?

ios - PHPhotoLibrary 在获取 placeholderForCreatedAsset 时崩溃

ios - 使用 PhotoLibrary 作为源时 UIImagePickerController 崩溃

objective-c - Xcode 属性自动化

objective-c - 跨一对一关系的核心数据获取请求谓词过滤

objective-c - 如何使大纲 View 中的文本单元格显示父行有多少个子行?

ios - 升级应用程序后,CoreData 是否保留数据?