如何使用swift检查String值是否在数组中

时间:2015-08-15 11:51:00

标签: ios arrays string swift

我试过了:

var array = ["led","zepp","jimmy","page"]

if array.contains("jimmy") { // error : [String] does not have a member named 'contains' .
    print("yes")
}

[String]类型数组的语法是什么?

1 个答案:

答案 0 :(得分:0)

取决于swift的版本......在1.2:

    var array = ["led","zepp","jimmy","page"]
    if find(array, "jimmy") != nil {
        println("yay")
    }

在swift 2中

   var array = ["led","zepp","jimmy","page"]
    if array.indexOf("jimmy") != nil {
        print("yay")
    }