ios 如何快速显示数组的所有元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24140099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:46:18  来源:igfitidea点击:

How to show all the elements of an array in swift?

iosxcodeswift

提问by user3725848

I have an array like this: var array = ["Chinese", "Italian", "Japanese", "French", "American"]

我有一个这样的数组: var array = ["Chinese", "Italian", "Japanese", "French", "American"]

I want to print out all separate elements on a new line.

我想在新行上打印出所有单独的元素。

How can I do this?

我怎样才能做到这一点?

回答by Viktor Nilsson

My personal favorite for debugging purposes is dump() which also prints which index the element has. Perfect if you have arrays within an array too.

我个人最喜欢的调试目的是 dump() ,它还会打印元素具有的索引。如果数组中也有数组,那就太完美了。

var array = ["Chinese", "Italian", "Japanese", "French", "American"]
dump(array)

This will generate the following output

这将生成以下输出

? 5 elements
  - [0]: Chinese
  - [1]: Italian
  - [2]: Japanese
  - [3]: French
  - [4]: American

回答by Bas

You can simply iterate through the arraylike this and print out all elements on a new line:

您可以像这样简单地遍历数组并在新行上打印出所有元素:

for element in array {
  println(element)
}

UPDATE

更新

For Swift 2 and Swift 3:

对于 Swift 2 和 Swift 3:

for element in array {
  print(element)
}

Or if you want it on the same line:

或者,如果您希望它在同一行:

for element in array {
  print(element, terminator: " ")
}

回答by Firo

Update:

更新:

Starting in iOS 9 you can now just use dump

从 iOS 9 开始,您现在可以使用 dump

var someArray = ["one", "two", "three"]
dump(someArray)

Original:

原来的:

This is a nice way to print arrays:

这是打印数组的好方法:

var someArray = ["one", "two", "three"]

// prints out the elements separated by a line break
// same as calling "println" on each item in the array:
println(someArray.joinWithSeparator("\n"))

// one
// two
// three

Otherwise if you want them on the same line you can just simply print the array:

否则,如果您希望它们在同一行,您可以简单地打印数组:

// prints on the same line:
// ["one", "two", "three"]
println(someArray)

回答by PDK

NB if you don't like the formatting of dump(…), then you can see from its signature that it has some defaulted parameters, thus will allow for some customization.

注意,如果您不喜欢 的格式dump(…),那么您可以从其签名中看到它具有一些默认参数,因此可以进行一些自定义。

Having said that, there are two more alternatives to consider that might provide more flexibility—for you to decide whether or not at the expense of readability!

话虽如此,还有两种替代方案可以考虑,它们可能会提供更大的灵活性——让您决定是否以牺牲可读性为代价!

First off, if you like closure argument notation, e.g. shorthand argument names, and/or you would like to first manipulate your array before you print it, you can use forEach:

首先,如果您喜欢闭包参数表示法,例如速记参数名称,和/或您想在打印之前先操作数组,则可以使用forEach

array.forEach() { print(
for (n, nationality) in array.enumerated() { print("\(n) \(nationality)") }
0 Chinese
1 Italian
2 Japanese
3 French
4 America
) } Chinese Italian Japanese French American array.sorted(by: { > ##代码##}).forEach() { print(##代码##) } American Chinese French Italian Japanese

Secondly, if you want an index but dump(…)is a bit too verbose, try:

其次,如果您想要一个索引但dump(…)有点过于冗长,请尝试:

##代码##

回答by Sami

You can also print the array as a string representation using println(arrayName.description)

您还可以使用 println(arrayName.description)