Skip to content

Instantly share code, notes, and snippets.

@pablogm
Last active February 11, 2018 09:31
Show Gist options
  • Save pablogm/7d7efe5cd269aa04f09e to your computer and use it in GitHub Desktop.
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, ...
//
// 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