简体   繁体   English

如何声明一个 Swift 数组

[英]How to declare a Swift Array

I'm trying to figure out my errors in a swift program it is my first program in swift and I'm learning the syntax.我试图找出我在 swift 程序中的错误,这是我在 swift 中的第一个程序,我正在学习语法。 How would one declare an array in swift?如何快速声明一个数组?

If you wish to declare an array in Swift there are two ways.如果你想在 Swift 中声明一个数组,有两种方法。 The most popular and most idiomatic is to use square brackets around a type.最流行和最惯用的方法是在类型周围使用方括号。 For an Array of Ints you might have:对于 Int 数组,您可能有:

var myArrayOfInts : [Int]

This is syntax sugar for using the generic Array struct so you could equivalently write:这是使用通用Array结构的语法糖,因此您可以等效地编写:

var myArrayOfInts : Array<Int>

Though I don't believe I have ever seen that written in any code (after the square bracket syntax was introduced) in any case that wasn't an example (just like this one) of how the two are equivalent.尽管我不相信我曾经见过用任何代码编写的(在引入方括号语法之后)在任何情况下都不是这两者如何等效的示例(就像这个示例)。

你应该试试

 var temp = [Int]()

很简单

var myArray: [String] = []

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

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