Skip to content

Instantly share code, notes, and snippets.

@KyleGoslan
Created February 2, 2018 13:48
Show Gist options
  • Save KyleGoslan/0ed7cbbb0a8ef638a4653eb20a787ebc to your computer and use it in GitHub Desktop.
Save KyleGoslan/0ed7cbbb0a8ef638a4653eb20a787ebc to your computer and use it in GitHub Desktop.
Swift Array Extension to merge unique values into an existing array. Elements must be conform to the Equatable protocol
//The array passed to the function is the array of object that will be omitted from the final array. Important if your merging an array of objects where the Equatable property may be the same but others may differ.
extension Array where Element: Equatable {
public mutating func mergeElements<C : Collection>(newElements: C) where C.Iterator.Element == Element{
let filteredList = newElements.filter({!self.contains($0)})
self.append(contentsOf: filteredList)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment