ios - Swift 中变量声明和定义的区别

标签 ios swift initialization initializer

术语“声明”和“定义”在 Apple 的 Swift 文档中被用作同义词,这让我感到困惑。

在“初始化”部分(讨论类初始化器)下,Apple 声明:

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.

他们在小节中进一步说明:

You can set the initial value of a stored property from within an initializer, as shown above. Alternatively, specify a default property value as part of the property’s declaration.

我认为变量声明不同于变量定义。

最佳答案

你是对的,这两个意思不同,虽然我认为大多数人只是以相同的意思使用它们,我认为那些 AppleDocs 也是如此。 Here is great article关于主题:

总结

A declaration provides basic attributes of a symbol: its type and its name. A definition provides all of the details of that symbol--if it's a function, what it does; if it's a class, what fields and methods it has; if it's a variable, where that variable is stored. Often, the compiler only needs to have a declaration for something in order to compile a file into an object file, expecting that the linker can find the definition from another file. If no source file ever defines a symbol, but it is declared, you will get errors at link time complaining about undefined symbols.

关于ios - Swift 中变量声明和定义的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31580432/

相关文章:

ios - Cocos3d : Set CC3Scene backdrop to transparent

ios - 在 iOS 上使用自动布局如何指定 View 应占用尽可能多的空间?

ios - 检查应用程序是否安装失败 iOS

ios - 取消隐藏标签栏 ios swift

android - Android TableLayout/TableRow 的 iOS/Swift 编程等效项

ios - 使用动态约束对齐文本字段

ios - MPMediaQuery 等到媒体对话框完成后再回调

C++:初始化变量;重载隐式构造函数?

c - 使用 {0} 或 {'\0' } 初始化结构之间有什么区别吗?

c++ - 正在使用未初始化的变量 C++