【发布时间】:2011-10-24 14:35:38
【问题描述】:
我的 sqlite 在其中一个实体中有项目,我希望根据它们的 categoryID 检索它们
例如categoryID 1 有 7 个项目, categoryID 2 有 5 个项目,当我点击一个 IBAction 时,它会来到这个方法并根据我的 global.clickedTag 检索
但我无法检索这些值,知道为什么吗?
-(NSMutableArray*)fetchItem:array{
[self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"IItem" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
//filter here
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"categoryID == %i",myGlobal.clickedTag];
[request setPredicate:predicate];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"ID" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
NSError *error;
NSMutableArray* mutableFetchCategory = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (!mutableFetchCategory) {
// Handle the error.
}
if ([mutableFetchCategory count] > 0)
{
for (NSManagedObject *info in mutableFetchCategory)
{
if ([[info valueForKey:@"categoryID"]intValue] == myGlobal.clickedTag)
{
NSLog(@"ID: %@", [info valueForKey:@"ID"]);
NSLog(@"category NAME/photo: %@", [info valueForKey:@"name"]);
NSLog(@"categoryID : %@",[info valueForKey:@"categoryID"]);
Item * i = [[Item alloc]initWithTitle:[info valueForKey:@"ID"] name:[info valueForKey:@"name"] description:[info valueForKey:@"desc"] thumbnail:[info valueForKey:@"thumbnail"] photo:[info valueForKey:@"photo"] categoryID:[info valueForKey:@"categoryID"]];
[array addObject:i];
NSLog(@"array count : %i",[array count]);
}
}
}
[mutableFetchCategory release];
[request release];
return array;
}
【问题讨论】:
-
你在哪里实例化返回值“数组”?在您的代码中找不到它。
标签: xcode sqlite core-data nspredicate nsfetchrequest