【问题标题】:How to find index of Int element in an array? [duplicate]如何在数组中找到 Int 元素的索引? [复制]
【发布时间】:2015-04-22 19:08:10
【问题描述】:

有没有“更好”(不那么冗长)的方式来做到这一点?

var found: Int?  
for i in 0...myArray-1 {
    if myArray[i] == 3 {
        found = i
    }
}

类似

let i = find(myArray, "1")

但是对于 Int ?

这个问题与How to find index of list item in Apple's swift? 略有“不同”,因为它直接针对 Int 而不是 String 的 find 函数。当然,唯一的区别是标题并指出您可以删除引号以获取 Int。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    这是一个 Int 示例

    let myArray = [1, 2, 3]
    var i = find(myArray, 2)
    

    祝你好运

    【讨论】:

    • 天哪,对不起,但谢谢!工作两个时区!
    • “工作两个时区”是什么意思? :)
    • find 返回一个可选的,所以你应该使用 if let i = find(myArray,2) {...}
    • “时区”:美国和欧洲
    猜你喜欢
    • 1970-01-01
    • 2012-11-24
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 2015-10-16
    • 2010-12-04
    • 2021-10-27
    相关资源
    最近更新 更多