我需要将使用我的核心数据图中的获取请求检索到的值放入一个数组中,但不完全确定如何进行此操作。
我正在使用以下内容执行获取:
NSString *entityName = @"Project"; // Put your entity name here
NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entityName);
// 2 - Request that Entity
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
// 3 - Filter it if you want
request.predicate = [NSPredicate predicateWithFormat:@"belongsToProject = %@", _selectedProject];
// 4 - Sort it if you want
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"dateTaken"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)]];
// 5 - Fetch it
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[self performFetch];
正如您所看到的,我正在使用NSPredicate过滤返回的值。
如何将这些值放入数组中,并且一旦它们在数组中,就能够为实体选择单独的属性,例如project.description或project.name?
感谢Eimantas我有一个数组中的对象,但是我仍然需要做两件事:
我正在使用以下for循环来完成第一个:
for (int i=0; i < [projectListArray count]; i++)
{
NSString *tmp = (NSString *)[projectListArray objectAtIndex:i];
}
但是,这会返回错误:
-[Project length]: unrecognized selector sent to instance 0x1b9f20
2012-03-28 10:48:35.160 Project App[3973:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Project length]: unrecognized selector sent to instance 0x1b9f20'
好像我可能不会递增?
答案 0 :(得分:1)
[self.fetchedResultsController fetchedObjects]
返回已获取对象的数组。
<强>更新强>
最好使用快速启动,而不是for
循环:
for (Project *project in projectListArray) {
NSString *projectDescription = [project valueForKey:@"description"];
}
您正在获取异常,因为您正在将对象转换为NSString
,而它是指向(我假设)Project
托管对象的指针。