Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Add non-updating insert methods to swift Set
import Foundation
extension Set {
mutating func nonUpdatingInsert(member: Element) {
if !self.contains(member) {
self.insert(member)
}
}
mutating func nonUpdatingUnionInPlace<S : SequenceType where S.Generator.Element == Element>(sequence: S) {
for item in sequence {
if !self.contains(item) {
self.insert(item)
}
}
}
}
Owner

simonracz commented Jan 8, 2016

Official insert method is actually an upsert. See http://www.chaonis.com/2016/01/swift-set-vs-nsmutableset/
This is a quick fix for those who want a non updating version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment