如何在Swift中声明和初始化数组 [英] How to declare and initialise an array in Swift
本文介绍了如何在Swift中声明和初始化数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Swift中有一个要编写的类,该类具有对象数组的变量。
I have a class in Swift that I'm trying to write that has a variable of an array of objects. Is there a better way to write this?
var myvar: Array<MyClass> = Array<MyClass>()
在没有=号之后,编译器会抱怨我的AppDelegate没有初始化程序。上面的方法似乎有点冗长(尽管我猜它并不比C#的简洁)。我只想知道是否有捷径。谢谢。
Without the bit after the = sign, the compiler complains about my AppDelegate having no initializers. The above way seems a bit lengthy (though it's no more terse than the c# equivalent, I guess). I'd just like to know if there's a shortcut. Thanks.
推荐答案
您可以使用空数组初始化变量:
You can initialize the variable with an empty array:
var myvar: Array<MyClass> = []
或使用自动类型推断:
var myvar = Array<MyClass>()
另外,您可以将 Array< MyClass>
编写为 [MyClass]
。
In addition, you can write Array<MyClass>
as [MyClass]
.
这篇关于如何在Swift中声明和初始化数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文