Black Sheep Black Sheep - 1 year ago 136
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?

Answer Source

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("")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download