当我在从Coredata Entity获取唯一用户数据时使用NSPredicate时,应用程序崩溃了。但是,没有错误日志。这是我正在使用的代码,
if (managedObjectContext == nil)
{
managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Name==%@",selectedUserName];
NSFetchRequest *fectchreq = [[NSFetchRequest alloc] init];
NSEntityDescription *entitydes = [NSEntityDescription entityForName:@"ALLData" inManagedObjectContext:managedObjectContext];
[fectchreq setEntity:entitydes];
[fectchreq setPredicate:predicate];
NSSortDescriptor *sortdes = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES];
NSArray *sortdesarray = [[NSArray alloc] initWithObjects:sortdes, nil];
[fectchreq setSortDescriptors:sortdesarray];
[sortdes release];
[sortdesarray release];
NSError *error = nil;
//NSMutableArray *storeddata = [[[managedObjectContext executeFetchRequest:fectchreq] error:&error] mutableCopy];
NSArray *storeddata = [managedObjectContext executeFetchRequest:fectchreq error:&error];
//[fectchreq release];
NSLog(@"StoreData : %@, count : %d",storeddata, [storeddata count]);
任何人都可以帮忙解决这个问题吗?提前谢谢。
答案 0 :(得分:0)
NSError *error;
NSMutableArray *storeddata = [managedObjectContext executeFetchRequest:fectchreq error:error];
您需要将此代码更改为:
NSError *error = nil;
NSArray *storeddata = [managedObjectContext executeFetchRequest:fectchreq error:&error];
原因:
error参数应该作为指针传递,因为声明是-executeFetchRequest:(NSFetchedRequest *)request error:(NSError **)error;
此方法也返回NSArray
,而不是NSMutableArray
- 您必须收到编译器警告...