ios NSPredicate在创建它时崩溃

时间:2012-07-20 13:04:07

标签: ios crash nsstring nspredicate nsfetchrequest

我正在使用此代码从Core数据中获取内容:

NSFetchRequest *req = [[NSFetchRequest alloc] initWithEntityName:@"Friend"];

NSString *predicateString = [NSString stringWithFormat:
                             @"identifier == %@", [[friendInfo objectForKey:@"id"] stringFromMD5]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString];
req.predicate = predicate;

req.fetchLimit = 1;
NSError *requestError = nil;
NSArray *tmp = [self.managedObjectContext executeFetchRequest:req error:&requestError];

但它崩溃了

NSString *predicateString = [NSString stringWithFormat:
                                 @"identifier == %@", [[friendInfo objectForKey:@"id"] stringFromMD5]];

如果我删除了stringFromMD5方法调用,它就可以工作。

NSString *predicateString = [NSString stringWithFormat:
                                     @"identifier == %@", [friendInfo objectForKey:@"id"]];

任何想法为什么? 感谢

1 个答案:

答案 0 :(得分:2)

请尝试以下方式,它可能会运作良好:

NSPredicate *_predicate = [NSPredicate predicateWithFormat:@"%K == %@", @"identifier", [[friendInfo objectForKey:@"id"] stringFromMD5]];