0
var locationsArray = [StudentLocation]()

I've got an array of StudentLocations and I'm trying to prevent any objects that are probably from the same person to be from the array.

People can make posts to a database with information about their name / location / profile URLs etc

The way the database is set up, people can repost in the same location under the same name as many times as they want, so one user might be adding 10 of the same posts to the username.

Each of these posts is going to have a different UID, but I think I can be fairly confident two posts are from the same user if the home location (lat / lon) and name are exactly the same.

What's the best way to prevent appending a location to the array if the home location / name is the same as another location already in the array?

user6820041
  • 1,213
  • 2
  • 13
  • 29
  • see this : https://stackoverflow.com/a/43798730/3901620 – KKRocks May 31 '17 at 04:27
  • If you add the Equatable (==) protocol to StudentLocation you can define the logic to determine if two objects are equal and then use a Set or contains method to filter duplicates. – Norman May 31 '17 at 05:16

1 Answers1

0

Use sets.

e.g. var temp:Set<Int> = [1,2,3,4,5,1,2] the result of this will be {2, 4, 5, 3, 1}.

You can use it as var locationsArray: Set<StudentLocation>. You can then use func insert(_ newMember: Element) -> (inserted: Bool, memberAfterInsert: Element) to insert a new object. If that object exists already, it won't add. You can find that from returned object by this method.

Dishant Kapadiya
  • 523
  • 4
  • 10