2014-01-29 32 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]; 
相關問題