0

当我在从 Coredata 实体获取唯一用户数据时使用 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]);

有人可以帮忙解决这个问题吗?提前致谢。

4

1 回答 1

0
NSError *error; 
NSMutableArray *storeddata = [managedObjectContext executeFetchRequest:fectchreq error:error];

您需要将此代码更改为:

NSError *error = nil;
NSArray *storeddata = [managedObjectContext executeFetchRequest:fectchreq error:&error];

原因:错误参数应该作为指针传递,因为声明-executeFetchRequest:(NSFetchedRequest *)request error:(NSError **)error; 也是这个方法返回NSArray,而不是NSMutableArray- 你必须得到关于这个的编译器警告......

于 2012-07-03T07:37:08.087 回答