我正在尝试创建一个 Core Data iPhone 应用程序。我正在跟踪的实体之一是汽车,每辆汽车的一个属性是“制造商”。
在我的应用程序的“编辑汽车”部分,我有一个 UIPickerView 需要加载每个先前已输入系统的唯一制造商。我要做的是创建一个 NSFetchRequest 来获取一组独特的“制造商”属性并使用它来填充 UIPickerView。
我遇到的问题是,无论数据存储中有 0 条记录还是 100 条记录,在元素 0 处执行的提取请求中总是有一条记录,其值为 @""。
我做错了还是有更简单的方法来做到这一点?我希望我可以运行一个快速的 sql 查询!!!
我的代码如下:
// Populate the manufacturerNameList array
NSManagedObjectContext *moc = [self.selectedLease managedObjectContext];
NSEntityDescription *ed = [NSEntityDescription entityForName:@"Car" inManagedObjectContext:moc];
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:ed];
// Get only manufacturer and only uniques
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"manufacturer",nil]];
[fetchRequest setReturnsDistinctResults:YES];
// Sort by manufacturer in ascending order
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"manufacturer" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error = nil;
self.manufacturerNameList = [moc executeFetchRequest:fetchRequest error:&error];
if (error) {
// Handle the error
}
NSLog(@"The count of self.propertyNameList is %i",[self.propertyNameList count]);
谢谢!