加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

可可 – 在斯威夫特进行倒计时?

发布时间:2020-12-14 05:37:34 所属栏目:百科 来源:网络整理
导读:这两个代码段有什么区别? let cell = tableView.dequeueReusableCellWithIdentifier("cellId") as UITableViewCell?// vslet cell = tableView.dequeueReusableCellWithIdentifier("cellId") as? UITableViewCell 结果不一样吗? 在该代码中没有什么区别,在
这两个代码段有什么区别?
let cell = tableView.dequeueReusableCellWithIdentifier("cellId") as UITableViewCell?
// vs
let cell = tableView.dequeueReusableCellWithIdentifier("cellId") as? UITableViewCell

结果不一样吗?

在该代码中没有什么区别,在这两种情况下,它评估为UITableViewCell?

真正的区别是:

>在第一种情况下,一个下降到UITableViewCell?预期总是成功(即使没有),所以如果dequeueReusableCellWithIdentifier返回的东西不是UITableViewCell(或继承自它的类的一个实例),那么它在运行时就会失败.该表达式返回一个可选的UITableViewCell?
>在第二种情况下,转换是可选的:如果dequeueReusableCellWithIdentifier返回的对象既不是UITableViewCell的实例也不是子类的实例,则downcast优雅地计算为nil(因此没有运行时错误).

当然dequeueReusableCellWithIdentifier总是返回一个UITableViewCell,这就是为什么你的代码没有区别.但是在其他情况下,差异可能存在,您必须注意防止运行时错误

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读