swift - 为什么swift中变量需要在类中初始化,而在struct中不需要初始化?

标签 swift

我刚学swift,最近发现变量或常量需要用初始数字或初始化器初始化,但在struct中不需要初始化?

最佳答案

他们确实需要。

来自 Swift 语言指南:

Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.

You can set an initial value for a stored property within an initializer, or by assigning a default property value as part of the property’s definition.

来源:Initialization

关于swift - 为什么swift中变量需要在类中初始化,而在struct中不需要初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34198828/

相关文章:

java - Xor 为 uint16 或 uint32 的字符串

swift - UIDataPicker 到 TextField

swift - 在 Buttons Swift 中回答

ios - 如何在 Sprite 套件中制作水平选择器 View 来选择不同的角色?

arrays - 重新保存 index.row 所选键的 NSUserDefaults 值

ios - 如何从另一个 swift 文件传输 viewTable 的数据源?

ios - 从粘贴板 IOS 检索视频

ios - NavigationBar Hide on Swipe 后,Cells 出现在 HeaderCell 和 Status Bar 之间一秒钟

ios - 如何使用循环访问对象数组的每个属性?

ios - 如何以编程方式快速设置物理定义?