2014-01-29 27 views
0

我已使用Google搜索,但无法找到解决方案 - 寻找一些帮助。需要从每个名称的倍数中获得一个名称列表(每个名称只有一个实例)

我有一个包括他们之间的不同位置和距离的数据库:

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接口 - 我只需要弄清楚如何最好地创建一个包含每个名称的数组(即“点A,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); 
} 

目前,它给我的结果 - 但他们结果是相似的: 这里是一个位置:点A 这里是一个位置:点A 这里是一个位置:pointB 下面是一个位置:pointB

我只是想获得该阵列读取,点A,pointB,pointC所以我应该只有3个位置(我将在稍后将这些位置放入UIPickverview中)。

我敢肯定,在我的逻辑中的东西是错误的 - 我只是无法弄清楚什么。

+0

的问题是,每个点多次出现在数组中。所以如果我删除了日志文本并且打印了数组,它会读取:pointA,pointA,pointB,pointB,pointC,pointC - 这就是我不想要的。 – Hanny

回答

1

的错误的逻辑是,你把你的数组中的对象的类型:

  • NSArray *fetchedLocationsLocationMiles
  • 数组你 要的是什么的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 
+0

谢谢你。我现在有一个NSSet,所有的学校都只有一次上市 - 正是我需要的。 – Hanny

0

你试过

[fetchedLocationsController.fetchRequest setReturnsDistinctResults:YES]; 
相关问题