【发布时间】:2024-01-04 05:29:01
【问题描述】:
看看这个声明from Apple document:
let optionalInt: Int? = 9
为什么要使用Int? 作为这个常量的类型?您知道它不能是nil,因为您将值 9 分配给它?可选类型的目的(据我了解)是能够保持nil. 在此语句中optionalInt 不需要保持nil。谁能解释一下?
【问题讨论】:
-
您能否链接您在其中找到该行代码的 Apple 文档页面?这将为我们提供更有效地回答的上下文。
-
@RaphBlanchet 我找到了; it's used as a single line example when introducing Optionals.
-
这里是文档的链接:developer.apple.com/library/ios/referencelibrary/GettingStarted/… 搜索“optionalInt”。这只是一个单行示例,但我希望这些示例有意义。
-
@DanBeaulieu 您可以将非可选的
Int添加到[Int?]。 -
@James 谢谢,下次有操场开放时我会试试的