当用户有按下登录按钮时,我想在用户输入用户名和密码时在警报视图中显示特定的名字。 请帮我。 提前致谢。 将不胜感激。
- (IBAction)LoginPressed:(id)sender
{
if (managedObjContext == nil) {
AppDelegate *app = (AppDelegate*)[UIApplication sharedApplication].delegate;
managedObjContext= app.managedObjectContext;
}
NSFetchRequest *request =[[NSFetchRequest alloc]init];
NSEntityDescription *rocordTableEntity = [NSEntityDescription entityForName:@"RecordTable" inManagedObjectContext:managedObjContext];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username==% @AND password==%@",L_usernameField.text,L_passwordField.text];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"firstname=%@",[managedObjContext valueForKey:@"firstname"]];
[request setEntity:rocordTableEntity];
[request setPredicate:predicate];
[request setPredicate:pred];
NSError *error;
NSArray *arrarforCheckingUserANDPass = [managedObjContext executeFetchRequest:request error:&error];
NSString *value = [arrarforCheckingUserANDPass valueForKey:@"firstname"];
if (arrarforCheckingUserANDPass.count != 0) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Welcome" message:value d elegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
else{
NSLog(@"not matched");
}
}
答案 0 :(得分:1)
你的第二个setPredicate
会覆盖第一个firstName
。你不需要它。您获得整个实体,然后只需访问NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"RecordTable"];
request.predicate = [NSPredicate predicateWithFormat:
@"username==%@ AND password==%@",L_usernameField.text,L_passwordField.text];
NSArray *result = [self.managedObjectContext executeFetchRequest:request error:nil];
RecordTable *user = result.firstObject;
if (user) {
NSLog(@"The first name is %@.", user.firstName);
}
属性。你的第一个谓词语法错误。
{{1}}顺便说一下,为什么你的方法以大写字母开头?这是一个非常糟糕的习惯。您的实体名称也是一场灾难。 “RecordTable”是您可以选择的最糟糕的实体名称。如果是用户,请将其命名为“用户”。遵循惯例,我也使用了firstName属性。