Last active
February 2, 2018 20:49
-
-
Save Daij-Djan/9d1c4b1233b4017f3b67 to your computer and use it in GitHub Desktop.
Added an Array extension with contains & indexOf -- bridging to NSArray is ugly and that hides it
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
import Foundation | |
extension Array { | |
func contains<U: Equatable>(object:U) -> Bool { | |
return (self.indexOf(object) != nil); | |
} | |
func indexOf<U: Equatable>(object: U) -> Int? { | |
for (idx, objectToCompare) in self.enumerate() { | |
if let to = objectToCompare as? U { | |
if object == to { | |
return idx | |
} | |
} | |
} | |
return nil | |
} | |
mutating func removeObject<U: Equatable>(object: U) { | |
let index = self.indexOf(object) | |
if(index != nil) { | |
self.removeAtIndex(index!) | |
} | |
} | |
} | |
yes, just uploaded a new version
IF you want to call that using a protocol .. e.g. MyProtocol, it won't work because you can't use a Protocol as a type constraint..
In that case I had to modify this to not use a generic but NSObjectProtocol
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Outdated as of Beta 5 :(