我用谷歌搜索但无法找到解决方案 - 寻求帮助。
我有一个包含不同位置和距离的数据库:
beg_location end_location miles
pointA pointB 2
pointA pointC 3
pointB pointA 2
pointB pointC 1
pointC pointA 3
pointC pointB 1
我正在使用MagicalRecord与CoreData接口 - 我只需要弄清楚如何最好地创建一个包含每个名称的数组(即“PointA,pointB,pointC”)
这是我的代码:
LocationMiles location;
//Create ResultsController
NSFetchedResultsController *fetchedLocationsController = [LocationMiles MR_fetchAllSortedBy:@"beg_location" ascending:YES withPredicate:nil groupBy:@"end_school" delegate:nil];
//turn controller into array
NSArray *fetchedLocations = [fetchedLocationsController fetchedObjects];
//go through array
for (location in fetchedLocations){
NSLog(@"Here is a location: %@", location.beg_location);
}
目前它给了我结果 - 但它们的结果类似于: 这是一个位置:pointA 这是一个位置:pointA 这是一个位置:pointB 这是一个位置:pointB
我只是想让数组读取为pointA,pointB,pointC,所以我应该只有3个位置(稍后我会把它们放到UIPickverview中)。
我确信我的逻辑中的某些东西在这里是错误的 - 我无法弄清楚是什么。
答案 0 :(得分:1)
错误的逻辑是你放入数组的对象类型:
NSArray *fetchedLocations
是LocationMiles
NSString
此外,您需要一个没有重复的对象集合。这就是NSSet
。
// NSSet ensures there's only one occurence of each object
NSMutableSet *locationsStrings = [[NSMutableSet alloc] init];
//go through array and add the field you're interested in into set
for (location in fetchedLocations){
[locationsStrings addObject:location.beg_location];
}
// make whatever use of locationsStrings you need
答案 1 :(得分:0)
你试过吗
[fetchedLocationsController.fetchRequest setReturnsDistinctResults:YES];