[英]compare coredata with string
在客观ci不能比较我的coredata与字符串
- (IBAction)loginBut:(id)sender {
AppDelegate *appDel = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDel managedObjectContext];
/*NSEntityDescription *newPassword = [NSEntityDescription insertNewObjectForEntityForName:@"Pinpass" inManagedObjectContext:context];
//[newPassword setValue:@"9090" forKey:@"password"];
//[context save:nil];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Pinpass"];
request.returnsObjectsAsFaults = NO;
NSArray *result = [context executeFetchRequest:request error:nil];
NSLog(@"%@", result);*/
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Pinpass"];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(password = %@)", loginField.text];
[request setPredicate:pred];
NSError *error;
NSArray *result = [context executeFetchRequest:request error:&error]; ;
if (result.count > 0){
//If([[textField text] compare:entity.attribute]==NSOrderedSame) NSLog(@"Correct!");
if ([[loginField text] isEqual:[result valueForKey:@"password"]])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sign in ok"
message:@"Sign in succsess!!"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
else{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sign in ok"
message:@"Sign in succsess!!"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Say Hello",nil]; }
}
我的代码有问题吗? 或只是对功能的错误使用
if ([[loginField text] isEqual:[result valueForKey:@"password"]])
result
是一个数组,因此您需要指定一个索引,然后检查该索引处的值。
例如,上面的行应类似于
if ([[loginField text] isEqual:[[result valueForKey:@"password"] lastObject]])
这里有几件事:
password
等于loginField.text
托管对象,因此根据定义,任何匹配项自动都是正确的密码。 result
是一个NSArray
。 大概您想将textField文本与该数组中的单个对象进行比较。 像这样: if (result.count > 0) { firstResult = [result firstObject]; if ([[loginField text] isEqual:[firstResult valueForKey:@"password"]]) { do stuff... } }
结果数组包含NSManagedObject名称Pinpass。 因此,您需要从中获取价值并进行比较...
if (result.count > 0){
Pinpass *pinObj=[result objectAtIndex:0];
if ([[loginField text] isEqual:[pinObj valueForKey:@"password"]])
{
//AlertView
}
else
{
//AlertView
}
}
希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.