困惑斯威夫特数组声明 [英] Confused about Swift Array Declarations
问题描述
有以下有何区别?
-
VAR array1_OfStrings = [字符串]()
VAR array2_OfStrings:[字符串] = []
在测试游乐场显示,1和2是相同的,但3的行为不同。
有人可以解释我的区别吗?而且这将是申报字符串
?
Testing in Playground shows that 1 and 2 are the same but 3 behaves differently.
Can someone explain me the difference please? And also what will be the preferred way to declare an empty array of String
?
推荐答案
首先两者具有同样的效果。
First two have the same effect.
-
声明一个变量
array1_OfStrings
,让它选择类型本身。当它看到[字符串]()
,它巧妙地知道这是字符串类型的数组。
declare a variable
array1_OfStrings
, let it choose the type itself. When it sees[String]()
, it smartly knows that's type array of string.
您设置变量 array2_OfStrings
作为字符串类型的数组,那你说这由 []
You set the variable array2_OfStrings
as type array of string, then you say it's empty by []
这是<强>不同的因为你只是告诉你想要 array3_OfStrings
是字符串类型的数组,但没有给它一个初始值
This is different because you just tell you want array3_OfStrings
to be type array of string, but not given it an initial value.
我认为第一个被推荐为<一个href=\"https://developer.apple.com/library/$p$prelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-ID107\"相对=nofollow>雨燕编程语言使用它更频繁。
I think the first one is recommended as The Swift Programming Language uses it more often.
这篇关于困惑斯威夫特数组声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!