Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@Daij-Djan Daij-Djan commented Jun 4, 2014

encapsulates bridging and hides NSNotFound workaround with an optional

@Daij-Djan

This comment has been minimized.

Copy link
Owner Author

@Daij-Djan Daij-Djan commented Jun 5, 2014

added getKeyPath for easy KVC

@irichardson

This comment has been minimized.

Copy link

@irichardson irichardson commented Aug 5, 2014

Outdated as of Beta 5 :(

@Daij-Djan

This comment has been minimized.

Copy link
Owner Author

@Daij-Djan Daij-Djan commented Jul 22, 2015

yes, just uploaded a new version

@Daij-Djan

This comment has been minimized.

Copy link
Owner Author

@Daij-Djan Daij-Djan commented Nov 30, 2015

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