One for the road. A simple array extension that returns the index of an object and removes an item
extension Array { func indexOfObject(object : AnyObject) -> NSInteger { return (self as NSArray).indexOfObject(object) } mutating func removeObject(object : AnyObject) { let index = self.indexOfObject(object) if (index != NSNotFound) { self.removeAtIndex(index) } } }
5 Comments
Erica, check this out: http://cl.ly/image/1V1A3G360V1h
In short, arrays in swift are mutable, but copied on length changes.
Thanks for testing that out!
It’s worth noting that the removeObject method on ObjC NSArray removes ALL occurrence of that object from the NSArray, while this only removes the first occurrence. I threw up something quick that should replicate the actual functionality of removeObject here: https://gist.github.com/ankushg/5c3fb3fb21cb1463875b
The real answer, of course, is to make your class adopt Equatable. Now you have a completely Swift-based solution: you can call find and removeAtIndex on an Array of that class.
What will `(self as NSArray)` do with an array containing value types?