Skip to content

Instantly share code, notes, and snippets.

@simonracz
Created January 8, 2016 13:19
Show Gist options
  • Save simonracz/fa2b9267ce07278d926d to your computer and use it in GitHub Desktop.
Save simonracz/fa2b9267ce07278d926d to your computer and use it in GitHub Desktop.
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)
}
}
}
}
@simonracz
Copy link
Author

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