arrays - 无法在 Swift 4 中使用 firstIndex:where
问题描述
当我在字典数组上Incorrect argument label in call (have 'where:', expected 'of:')
使用时出现错误。firstIndex
let d: [NSMutableDictionary] = [["u": 1], ["u": 2], ["u": 3]]
let i = d.firstIndex(where: { dict -> Bool in
return dict["u"] == 2
})
为什么会发生这种情况以及如何解决?
解决方案
Swift 无法知道是否== 2
可以使用dict["u"]
. 你知道这dict["u"]
是一个 Int,但Swift不知道,因为通过将这些字典键入为 NSMutableDictionary,你已经隐藏了值的类型。
要解决此问题,请更改[NSMutableDictionary]
为[[String:Int]]
.
推荐阅读
- django - 分析django输入表单
- vba - 从特定行开始选择多列
- mysql - MYSQL 函数根据列的连续值返回记录
- javascript - 为 CanvasJS 图表插入数组变量
- node.js - 命令或方法清除 mongodb 或 mongoose 中的所有集合
- url - 三级域和路径之间的区别
- php - 需要帮助在 php 中转换 strtotime()
- javascript - React JS 路由不起作用(Uncaught TypeError: Super expression must be null or a function, not object)
- r - 根据另一个向量的值创建向量
- asp.net-core-webapi - 使用文件发布表单数据会导致“输入无效”错误