无法将我的核心数据与字符串进行比较
- (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]; }
}
我的代码有问题吗?或者只是对功能的错误使用
答案 0 :(得分:0)
if ([[loginField text] isEqual:[result valueForKey:@"password"]])
result
是一个数组,因此您需要指定一个索引,然后检查该索引处的值。
例如,上面的行应该是
if ([[loginField text] isEqual:[[result valueForKey:@"password"] lastObject]])
答案 1 :(得分:0)
这里有几件事:
password
等于loginField.text
的托管对象,因此根据定义,任何匹配都会自动为正确的密码。result
是NSArray
。大概你想要将textField文本与该数组中的单个对象进行比较。这样的事情: if (result.count > 0) {
firstResult = [result firstObject];
if ([[loginField text] isEqual:[firstResult valueForKey:@"password"]]) {
do stuff...
}
}
答案 2 :(得分:0)
结果数组包含NSManagedObject名称Pinpass。所以你需要从中获取值并进行比较......
if (result.count > 0){
Pinpass *pinObj=[result objectAtIndex:0];
if ([[loginField text] isEqual:[pinObj valueForKey:@"password"]])
{
//AlertView
}
else
{
//AlertView
}
}
希望它可以帮助你......!