简体   繁体   English

在iOS Swift中声明变量之间的区别?

[英]Difference between declaring a variable in ios Swift?

I am learning Swift recently and I found that there are two types of declaration for a variable. 我最近在学习Swift,发现变量有两种类型的声明。

var indexArray = NSMutableArray()     //and
var indexArray : NSMutableArray = NSMutableArray()

Am just wondering what is the difference between them? 只是想知道它们之间有什么区别? Will it replicate in any kind of assigning values to the variable? 它将以任何形式给变量赋值吗?

Here is a simple explanation 这是一个简单的解释

var indexArray = NSMutableArray()

As the above, indexArray variable can be any one , String , Int , ....... You didn't specifically give any type for that variable. 如上所述,indexArray变量可以是String,Int,.......中的任何一个。您没有专门为该变量指定任何类型。

var indexArray : NSMutableArray = NSMutableArray()

In here you specifically give that indexArray is a NSMutableArray 在这里,您专门给出indexArray是NSMutableArray

You can provide a type annotation when you declare a constant or variable, to be clear about the kind of values the constant or variable can store. 声明常量或变量时,可以提供类型注释,以明确常量或变量可以存储的值的类型。 Write a type annotation by placing a colon after the constant or variable name, followed by a space, followed by the name of the type to use. 通过在常量或变量名称后加一个冒号,后跟一个空格,后跟要使用的类型的名称来编写类型注释。

This example provides a type annotation for a variable called welcomeMessage, to indicate that the variable can store String values: 本示例为名为welcomeMessage的变量提供类型注释,以指示该变量可以存储String值:

 var welcomeMessage: String

The colon in the declaration means “…of type…,” so the code above can be read as: 声明中的冒号表示“…类型……”,因此上面的代码可以读作:

Declare a variable called welcomeMessage that is of type String. 声明一个名为welcomeMessage的字符串类型的变量。

The phrase “of type String” means “can store any String value.” Think of it as meaning “the type of thing” (or “the kind of thing”) that can be stored. 短语“字符串类型”的意思是“可以存储任何字符串值。”将其视为可以存储的“事物的类型”(或“事物的类型”)。

The welcomeMessage variable can now be set to any string value without error: 现在可以将welcomeMessage变量设置为任何字符串值,而不会出现错误:

 welcomeMessage = "Hello" 

You can define multiple related variables of the same type on a single line, separated by commas, with a single type annotation after the final variable name: 您可以在一行上定义多个相同类型的相关变量,并用逗号分隔,并在最终变量名称后添加一个类型注释,以单行表示:

var red, green, blue: Double”

* Note * * 注意 *

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, as described in Type Safety and Type Inference. 如果在定义时为常量或变量提供初始值,Swift几乎总能推断出用于该常量或变量的类型,如类型安全性和类型推断中所述。 In the welcomeMessage example above, no initial value is provided, and so the type of the welcomeMessage variable is specified with a type annotation rather than being inferred from an initial value. 在上面的welcomeMessage示例中,没有提供任何初始值,因此welcomeMessage变量的类型是通过类型注释指定的,而不是从初始值推断出来的。

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2 Prerelease).” iBooks. 摘自:Apple Inc.“ Swift编程语言(Swift 2预发行版)。” iBooks。 https://itun.es/us/k5SW7.l https://itun.es/us/k5SW7.l

One of the important language feature of Swift is Type Inference . Swift是语言的重要特征之一是类型推断 What it means is that a variable can identify what is its type based on the value its assigned with. 这意味着变量可以基于为其分配的值来识别其类型。

var indexArray = NSMutableArray()

By the above statement, it is implicitly known that the variable indexArray is of type NSMutableArray without being specified. 通过上面的陈述,可以隐含地知道变量indexArray的类型为NSMutableArray而未指定。

Whereas in the other type of declaration, 而在另一种声明中,

var indexArray : NSMutableArray = NSMutableArray()

you are explicitly specifying that the variable indexArray is of type NSMutableArray before assigning a value to it. 您在将变量indexArray之前,已明确指定变量indexArrayNSMutableArray类型。 If you assign a different type to it, the compiler is throw an error. 如果为它分配其他类型,则编译器将引发错误。

var indexArray : NSMutableArray = NSString() // This is wrong 

A very good starting point is to go over the Swift Language document by Apple. 一个很好的起点是翻阅 Apple的Swift语言文档

No difference, both are same.. 没什么区别,都是一样的。

there is difference between the follwing declarations, 以下声明之间存在差异,

var welcomeMessage: String? var welcomeMessage:字符串? - can be nil or assigned nil at any point of time -可以在任何时间点为零或分配为零

var welcomeMessage: String! var welcomeMessage:字符串! - can never be nil at any point of time when using that variable. -使用该变量时,在任何时间都不能为零。

var welcomeMessage: String - will throw error saying, class has no initializers var welcomeMessage:字符串-将抛出错误消息,类没有初始化程序

But there is no difference between the following two, 但是以下两个之间没有区别,

var welcomeMessage: String = String() var welcomeMessage:字符串= String()

var welcomeMessage = String() var welcomeMessage = String()

In the first case, indexArray takes the type of the assignment source automatically, so in your example case the statements are exactly the same. 在第一种情况下,indexArray自动获取赋值源的类型,因此在您的示例情况下,语句完全相同。

The difference besides the second version being more explicit is that you could tweak the type of indexArray - for example to a base class - in the second case; 除了第二个版本更明确之外,不同之处在于,在第二种情况下,您可以调整indexArray的类型,例如,调整为基类。

var indexArray = NSMutableArray()            // indexArray is type NSMutableArray

var indexArray : NSArray = NSMutableArray()  // indexArray is type NSArray
                                             // but contains an NSMutableArray

Of course, if you declare a variable without assigning it you can't take the type automatically from the assignment source, so then you need the explicit type declaration. 当然,如果在不分配变量的情况下声明变量,则无法从赋值源自动获取类型,因此需要显式类型声明。

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

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