arrays - swift 3 : Remove value in Array with Unknown index

标签 arrays swift

我想在我的 Shinobi DataGrid 中实现多次点击。我有一个有数组的网格

( ["1", "32", and more] )

如果我单击网格,我会将其放入新数组 self.arrayNr.append(currNr)

但我想检查并删除 currNr 是否已经存在于 arrayNr 中,它将从 arrayNr 中删除。

我是新手,正在使用 Swift 3。我读了一些关于我的问题的问题,比如 thisthis但它不工作。我认为 Swift 2 在处理 String 方面比 Swift 3 更简单。有什么建议或答案对我有帮助吗?

最佳答案

您可以使用 index(of 来检查数组中是否存在 currNr。(该类必须符合 Equatable 协议(protocol))

var arrayNr = ["1", "32", "100"]
let currNr = "32"
// Check to remove the existing element
if let index = arrayNr.index(of: currNr) {
    arrayNr.remove(at: index)
}
arrayNr.append(currNr)

关于arrays - swift 3 : Remove value in Array with Unknown index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45002768/

相关文章:

ios - 从下往上填充UIView

swift - 使用 NSDate() 作为 Parse.com 的键保存字典失败

php - 根据特定键在对象数组中查找重复项

c - 取消引用指向 void 数组的指针

javascript - 按预期顺序检索 GET 请求响应时出现问题

xcode - 如何使用本地数据存储通过 parse 和 swift 保存和查询数据

ios - Swift 的新手和困惑 - 在 Swift 类中存储大型常量/数据集

ios - 具有存储枚举的条件枚举开关

arrays - 消除数组中的空格,它不能快速工作

angularjs - Angular 2 : cannot read property 'push' of undefined