简体   繁体   English

Swift中变量声明和定义之间的区别

[英]Difference between variable declaration and definition in Swift

The terms "declaration" and "definition" are being used synonymously in Apple's Swift documentation and it's getting me confused. 术语“声明”和“定义”在Apple的Swift文档中被同义使用,这让我感到困惑。

Under the "Initialization" section (which talks about class initializers ), Apple states: 在“初始化”部分(谈论类初始化程序 )下,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 . 您可以在初始化程序中为存储的属性设置初始值,也可以通过将默认属性值指定为属性定义的一部分来设置初始值。

Further in a subsection they state: 在一个小节中,他们还说:

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 . 或者,将默认属性值指定为属性声明的一部分

I thought a variable declaration was different than a variable definition. 我认为变量声明与变量定义不同。

You are right that those two mean different thing, THOUGH I think most of the people just use both of them in the same meaning and I think that is also the case of those AppleDocs. 你是对的,这两个意味着不同的东西,我认为大多数人只是以相同的含义同时使用它们,我认为那些也是AppleDocs的情况。 Here is great article on subject: 这是关于主题的精彩文章

Summary 摘要

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. 如果没有源文件定义符号,但声明了它,则会在链接时收到错误,抱怨未定义的符号。

After doing much searching across the web for legitimate explanations, I have seemed to have found an answer: 在网上搜索了大量合法解释后,我似乎找到了答案:

The problem is that the two terms overlap to some extent. 问题是这两个术语在某种程度上重叠。 Definitions also serve as declarations, because they inject an identifier of a certain type to a scope. 定义也用作声明,因为它们将特定类型的标识符注入范围。 However, a declaration isn't a definition because it doesn't entail storage allocation for the declared object. 但是,声明不是定义,因为它不需要为声明的对象进行存储分配。 To add to the confusion, the semantics of definitions and declarations is slightly different when applied to types and functions, as I will show momentarily. 为了增加混淆,定义和声明的语义在应用于类型和函数时略有不同,我将暂时展示。 So let's look at a more detailed analysis of these two terms. 那么让我们来看看这两个术语的更详细的分析。

Here is the article: Declarations and Definitions . 这是文章: 声明和定义

The article gives further explanation and examples. 本文给出了进一步的解释和例子。

Declaration of variable mean to tell compiler their is a var\\funct\\struct of particular data type. 变量声明意味着告诉编译器它们是特定数据类型的var \\ funct \\ struct。 Definition of variable mean asking compiler to allocate memory to variable or define storage for that variable. 变量的定义意味着要求编译器将内存分配给变量或定义该变量的存储。 you can define a variable only one time but you can declare it as many time you want. 您只能定义一次变量,但您可以根据需要多次声明它。

I think Apple's Swift 4 Language Reference can be construed as the authoritative answer. 我认为Apple的Swift 4语言参考可以被解释为权威的答案。 From the Declarations section (emphasis mine): 声明部分(强调我的):

A declaration introduces a new name or construct into your program. 声明在程序中引入了新名称或构造。 For example, you use declarations to introduce functions and methods, variables and constants, and to define new, named enumeration, structure, class, and protocol types. 例如,您使用声明来引入函数和方法,变量和常量,以及定义新的命名枚举,结构,类和协议类型。 You can also use a declaration to extend the behavior of an existing named type and to import symbols into your program that are declared elsewhere. 您还可以使用声明来扩展现有命名类型的行为,并将符号导入到其他地方声明的程序中。

In Swift, most declarations are also definitions in the sense that they are implemented or initialized at the same time they are declared. 在Swift中,大多数声明也是定义 ,因为它们是在声明它们的同时实现或初始化的。 That said, because protocols don't implement their members, most protocol members are declarations only. 也就是说,由于协议不实现其成员,因此大多数协议成员仅是声明。 For convenience and because the distinction isn't that important in Swift, the term declaration covers both declarations and definitions . 为方便起见,因为在Swift中区别并不重要,术语声明包括声明和定义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM