Swift从数组中删除给定值

时间:2014-11-04 20:58:36

标签: iphone swift ios8

我有一个带

的简单数组
var specs : [String] = [Button1,Button2,Button3,Button4]

如何在我的erray中找到某个值(类型为Button3),从而取消?

1 个答案:

答案 0 :(得分:2)

您可以使用find()全局函数来检索索引:

var specs : [String] = ["Button1","Button2","Button3","Button4"]

let index = find(specs, "Button3")

然后,一旦验证该元素存在(find返回一个可选项),请将其删除:

if let index = index {
    specs.removeAtIndex(index)
}