Created
February 2, 2018 13:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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