gpt4 book ai didi

ios - 如何选择核心数据中的特定列?

转载 作者:行者123 更新时间:2023-11-29 02:58:30 24 4
gpt4 key购买 nike

当用户按下登录按钮时,我想在用户输入用户名和密码时在警报 View 中显示特定的名字。请帮我。提前致谢。我们将不胜感激。

    - (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");
}



}

最佳答案

您的第二个 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);
}

顺便说一句,为什么你的方法以大写字母开头?这是一个非常不好的习惯。您的实体名称也是一场灾难。 “RecordTable”是您可以选择的最糟糕的实体名称。如果是用户,就称它为“用户”。按照惯例,我也将您的 firstName 属性进行了驼峰式大小写。

关于ios - 如何选择核心数据中的特定列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23579264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com