1

I am working on a Map application. I have Points of Interest array, which I would have to display on the Map. When I display these points in a tableView, they have to be unique, but when I select one of them, I have to pass all the points so that they can be displayed on the map.

struct POI {
   let id: String
   let name: String
   let floorName: String
}

For this struct, I am getting [POI] from the service, which has the same names and floorName for certain elements, but different ids. From the UI perspective, there is no point showing POI with the same name and floorName more than once. However, when the user selects one, I have to pass all the elements with the same name and floorName and different IDs, so that the map can show all of them.

Currently, I am using Dictionary grouping to find unique values, but was just wondering if there is any better way.

Vandan Patel
  • 1,012
  • 1
  • 12
  • 23
  • can you show the code of the Dictionary and the display method? – Olympiloutre Jul 09 '19 at 01:11
  • ```let poisByname = Dictionary(grouping: pois) { $0.name } for(_, uniqueByname) in poisByname { let poisByFloor = Dictionary(grouping: uniqueByname) { $0.floorName } }``` I hope this helps. – Vandan Patel Jul 09 '19 at 02:55
  • @VandanPatel Here's the answer to the question you just deleted (just in reverse): https://stackoverflow.com/questions/29321947/xcode-swift-am-pm-time-to-24-hour-format (I'll delete this comment soon). – rmaddy Aug 07 '19 at 03:41

1 Answers1

2

The usual way of uniquing a bunch of values is to put them into a Set. A Set can contain only one of a bunch of equivalent values.

What I would do in your case is declare another struct. Let's call it POInoID:

struct POInoID : Hashable {
   let name: String
   let floorName: String
   init(poi:POI) { self.name = poi.name; self.floorName = poi.floorName }
}

Now you map all your POI objects to POInoID objects and put all the POInoID objects into a Set. Presto, all objects in the Set are unique.

matt
  • 515,959
  • 87
  • 875
  • 1,141