gpt4 book ai didi

ios - Swift 括号中的赋值

转载 作者:搜寻专家 更新时间:2023-11-01 06:38:58 26 4
gpt4 key购买 nike

Swift 初学者就在这里。我正在阅读 Swift 的教科书,发现一个奇怪的表达方式……这段代码(第二行“let”)在做什么?请看一下等号和 UITableVIewCell 方法之间的关系。对我来说,它看起来像“c 不是 nil,它应该是可选的,并且 c 应该被展开......”

(c != 无) ? c!

我很难在互联网 (google) 上搜索它,因为我无法在搜索引擎中为该问题制定一个好的搜索关键字。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//create cells in table
let c = tableView.dequeueReusableCellWithIdentifier("table_cell")
let cell = (c != nil) ? c!: UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "table_cell")
return cell
}

最佳答案

线

let cell = (c != nil) ? c!: UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "table_cell")

正在展示 ? ternary operator .

如果 c != nil 则返回 c!,否则调用 UITableViewCell

代码可以简化为使用速记 nil 合并运算符:

let cell = c ?? UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "table_cell")

类似问题/答案 here .

关于ios - Swift 括号中的赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37915261/

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