gpt4 book ai didi

ios - Swift 获取 UITableView 数组内值的第一个实例

转载 作者:行者123 更新时间:2023-11-30 11:21:02 27 4
gpt4 key购买 nike

我有一个模型数组,每个模型都有一个链接到其父级的id。我试图获取此 id 出现在数组内的第一个实例,并将单元格导出属性设置为 false。

该数组用于填充 TableView ,我当前正在使用 TableView 的索引进行检查。尝试使用 .first() 方法,但它抛出错误:

Cannot invoke 'first' with an argument list of type '(where: (Course) throws -> Bool)'

if courses.first(where: {$0.id == index}) {
controller.courseTitleGroup.setHidden(true)
} else {
controller.courseTitleGroup.setHidden(false)
}

逻辑应该是:如果单元格索引== course[index].id及其第一个实例,则返回true

谢谢

最佳答案

你可以通过这样做来简化

let found = courses.first(where: {$0.id == index}) != nil
controller.courseTitleGroup.setHidden(found)

你不能像你那样做,因为first(where:)返回一个可选的Course,而不是Bool; if 语句只允许 Bool 值,因此您可以将可选返回值与非 nil 进行比较。

关于ios - Swift 获取 UITableView 数组内值的第一个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51269197/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com