1

我面临使用 predicateWithFormat 方法创建 NSPredicate 类型对象的问题。

AppDelegate *appdelegateObj = (AppDelegate *)[[UIApplication sharedApplication] delegate];

self.managedObjectContext = [appdelegateObj managedObjectContext];
NSString *emailWithBackslashedAtTheRate = [email stringByReplacingOccurrencesOfString:@"@" withString:@"\\@"];

NSFetchRequest *fetchRequestObj = [NSFetchRequest fetchRequestWithEntityName:@"Usertable"];
NSMutableArray *dataList;

**NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"email CONTAINS[c] %@",emailWithBackslashedAtTheRate]];**
[fetchRequestObj setPredicate:predicate];

@synchronized(delegate.persistentStoreCoordinator) {

    NSError *error = nil;
    NSMutableArray *filteredUser = (NSMutableArray *)[self.managedObjectContext executeFetchRequest:fetchRequestObj error:&error];
}

谁能解释一下,有什么问题吗?

4

2 回答 2

2

在您的用户表中,字段名称电子邮件必须是字符串。在此处输入图像描述

 AppDelegate *coreAppDelegate=[[UIApplication sharedApplication]delegate];
        NSManagedObjectContext *context=[coreAppDelegate managedObjectContext];
        NSEntityDescription *entityDecs=[NSEntityDescription entityForName:@"UserDetail" inManagedObjectContext:context];
        NSFetchRequest *request =[[NSFetchRequest alloc]init];
        [request setEntity:entityDecs];
        //  NSLog(@"username = %@",mutarr);
        NSPredicate *pred=[NSPredicate predicateWithFormat:@"(email=%@)",TxtEmail];
        [request setPredicate:pred];
        NSManagedObject *matches =nil;
        NSError *error;
于 2015-12-30T07:00:28.077 回答
0

您不能使用该NSString方法stringWithFormat来构建用于谓词的格式字符串。尽管名称相似,predicateWithFormat工作stringWithFormat方式不同。您应该将正确的格式字符串直接传递给predicateWithFormat

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"email CONTAINS[c] %@", email];

(此实例中的主要区别在于predicateWithFormat将由 %@ 表示的字符串括在单引号中,而stringWithFormat没有。)

于 2015-12-30T17:49:34.527 回答