创建空数组时为什么要添加() [英] why should we add () when we create a empty array
本文介绍了创建空数组时为什么要添加()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们可以这样创建一个空数组:
we can create an empty array like this:
var newArray1 = [Int]()
我的意思是:当我们创建一个新数组时,()在这里意味着什么,为什么不创建一个新的空数组呢?通过写这个数组:
I mean: what "()" means here when we create a new array, why don't we just create a new empty array by writing this:
var newArray2 = [Int]
我知道它的语法,但是可以有人解释一下以帮助我深入理解它。
I know it's syntax, but can somebody explain it a little to help me understand it deeply.
推荐答案
要创建一个新数组,可以使用 Array< Int> .init()
。调用初始化程序的简便方法是删除 .init
: Array< Int>()
。
To create a new array, you would use Array<Int>.init()
. The shorthand for calling initializers is to just drop the .init
: Array<Int>()
.
[Int]
是 Array< Int>
的简写。
将两者放在一起,我们可以将 Array< Int> .init()
编写为 [Int]()
。
Putting the two together, we can write Array<Int>.init()
as [Int]()
.
这篇关于创建空数组时为什么要添加()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文