gpt4 book ai didi

swift - swift 中这两种类型的类型转换方法有什么区别

转载 作者:行者123 更新时间:2023-11-28 06:51:17 25 4
gpt4 key购买 nike

.. 之间有什么区别(假设这甚至是类型转换)

let cell = tableView.cellForRowAtIndexPath(indexPath) as! MessageCell

let cell:MessageCell = tableView.cellForRowAtIndexPath(indexPath)

我知道这看起来很基础,但我真的不知道我在问什么,以至于无法找到合适的资源来帮助我理解这一点。

最佳答案

第一个表达式是强制向下转型

有两种情况:

  • 返回类型是AnyObject,必须转换为正确的类型;
  • 返回类型是基类 (UITableViewCell),必须转换为子类 (MessageCell)。

第二个表达式不是类型转换,它是一个类型注解

用过

  • 如果无法从默认值推断类型,则声明类型。

    来自文档

    It is rare that you need to write type annotations in practice. If you provide an initial value for a constant or variable at the point that it is defined, Swift can almost always infer the type to be used for that constant or variable

关于swift - swift 中这两种类型的类型转换方法有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34849223/

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