As Romain suggested, you can use a Swift array. If you continue to use NSMutableArray , you can do either:
for object in vehicles { if let vehicle = object as? Vehicle { print(vehicle.registration) } }
or, if you only know Vehicle objects, you can force it to expand:
for vehicle in vehicles { print((vehicle as! Vehicle).registration) }
or as DNagy suggested:
for vehicle in vehicles as NSArray as! [Vehicle] { print(vehicle.registration) }
Obviously, if possible, the question arises: can you retire NSMutableArray and use Array<Vehicle> (aka [Vehicle] ). So, instead of:
let vehicles = NSMutableArray()
You can do:
var vehicles = [Vehicle]()
Then you can do things like:
for vehicle in vehicles { print(vehicle.registration) }
Sometimes we are stuck with Objective-C code that returns NSMutableArray objects, but if this NSMutableArray was created in Swift, it is probably preferable to use Array<Vehicle> .
Rob
source share