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("")
Try this
var array = ["", "2", "3"]
let index = array.indexOf("")
or you can use the NSArray
method:
var array: [AnyObject] = ["", "2", "3"]
let index = (array as NSArray).indexOfObject("")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With