Black Sheep Black Sheep - 4 months ago 30
Swift Question

indexOf of AnyObject array not working with Strings

So I'm having the following code in a playgroung

var array: [AnyObject] = ["", "2", "3"]

let index = array.indexOf("")

And XCode is marking a compiler error

Cannot convert value of type 'String' to expected argument type '@noescape (AnyObject) throws -> Bool'

So my question is how do I get the indexOf an element in an Array in of AnyObjects?


You can also cast to [String] if you're sure it will cast safely

jvar array: [AnyObject] = ["", "2", "3"]
let index = (array as! [String]).indexOf("")