senty senty - 1 year ago 133
Swift Question

Check if NSMutableArray has Value

I have an

that is stored with
values. I have a String variable and I want to check if it contains the value or not.

I tried using
with String but it says..

Cannot convert value of type String to expected argument type...

var mutableArray = NSMutableArray() // ["abc", "123"]
var string = "abc"

mutableArray.contains("abc") { // above error in this line


Answer Source

I do still not understand why you cannot use a native Swift array, but okay.

Two possible solutions are to either use

let contains = mutableArray.contains { $0 as? String == "abc" }


let contains = mutableArray.indexOfObject("abc") != NSNotFound

If you would use a native array you could simply do

var array = ["123", "abc"]
let contains = array.contains("abc")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download