我的ViewController中的Hello:
[super viewDidLoad];
context = [[AppDelegate sharedAppDelegate] managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Items" inManagedObjectContext:context]];
NSArray *results = [context executeFetchRequest:request error:&error];
if(error){
NSLog(@"++++++++");
return;
}
NSLog(@"ARRRAY====%@",[results valueForKey:@"items"] );
数据是:
("null>", MyGoodData)
如果我在AppDelegate中调用此代码,那么ok。:
数据是:
(MyGoodData)
答案 0 :(得分:10)
首先,您的错误检查是错误的。你应该检查
if (results == nil) {
//Error condition
}
不检查error
的结果。即使在非错误情况下,也可以填充error
。
其次,results
的值为NSArray
,这意味着密钥items
没有值。您可以直接打印出数组的结果:
NSLog(@"ARRAY ==== %@", results);
欢迎使用stackoverflow。
答案 1 :(得分:0)
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Items"];
[request setEntity:[NSEntityDescription entityForName:@"Items" inManagedObjectContext:context]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"items > 0"];
[request setPredicate:predicate];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"range" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObjects:sort, nil]];
NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];
答案 2 :(得分:-2)
解决方案:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"items > 0"];
[request setPredicate:predicate];