Last active
February 11, 2018 09:31
-
-
Save pablogm/7d7efe5cd269aa04f09e to your computer and use it in GitHub Desktop.
Swift Array Utils: append an element if it doesn't exist, retrieve the latest element, ...
This file contains hidden or 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
// | |
// Array+Utils.swift | |
// Swift Utils | |
// | |
import Foundation | |
extension Array where Element: Hashable { | |
mutating func filterAppend(newElement: Element) { | |
for item in self { | |
if item.hashValue == newElement.hashValue { | |
return | |
} | |
} | |
self.append(newElement) | |
} | |
} | |
extension Array { | |
var last: Element { | |
return self[self.endIndex - 1] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment