Skip to content

Instantly share code, notes, and snippets.

@Daij-Djan
Last active February 2, 2018 20:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daij-Djan/9d1c4b1233b4017f3b67 to your computer and use it in GitHub Desktop.
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
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!)
}
}
}
@Daij-Djan
Copy link
Author

encapsulates bridging and hides NSNotFound workaround with an optional

@Daij-Djan
Copy link
Author

added getKeyPath for easy KVC

@irichardson
Copy link

Outdated as of Beta 5 :(

@Daij-Djan
Copy link
Author

yes, just uploaded a new version

@Daij-Djan
Copy link
Author

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