swift 最后(:where) element in an array

标签 swift

对于数组:

var arr = [3,2,1,2,1,4,5,8,6,7,4,2]

我能找到第一个匹配的元素

arr.first(where: {$0 == 2})! 

但最后

arr.last(where: {$0 == 2})

返回:

cannot call value of non-function type 'Int?'

为什么?

最佳答案

last(where:) 已添加到 Swift 4.2 (Xcode 10) 中。

如果您需要使用 Swift 4.1 或更早版本查找最后一个匹配项,您可以执行以下操作:

let last = arr.reversed().first { $0 == 2 }

关于 swift 最后(:where) element in an array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52141976/

相关文章:

ios - 将 json 文件读入 CoreData 时出现精度错误

ios - 获取有关当前模拟器设备的信息

ios - 如何使用 writeToFile 将图像保存在文档目录中?

ios - 【UISearchController/UISearchBar】如何在输入文字时一直显示书签按钮?

ios - 为什么我们不能直接在 oc 或 swift 中捕获 Kotlin 代码产生的异常?

ios - 使用 Firebase 的本地通知

arrays - 在 Swift 中创建包含不同类型的多维数组

swift - 让常量在类中的其他函数中看不到

ios - 在 iOS 中调试时推送 viewController 不工作

ios - 在 Collection View 中显示三个单元格