迭代数组只获得Swift Playground中的第一个值

时间:2023-01-23 19:12:37

I'm aware this is a pretty basic question but it has me stumped and figure out why. I have an array of names and what to iterate through them to get them all as single names, although when I do so using the for in loop it just produce the first name, my code:

我知道这是一个非常基本的问题,但它让我难以理解并找出原因。我有一个名称数组和迭代它们以将它们全部作为单个名称,但是当我使用for in循环时,它只生成第一个名称,我的代码:

var arrayA = ["Mike", "James", "Stacey", "Steve"]
var str = String()
for array in arrayA {
    str = array 
    println(str)//Just prints steve
}

The problem is it only prints steve, I want to be able to access all the name singley

问题是它只打印史蒂夫,我希望能够访问所有名称单身

This is what I am seeing:

这就是我所看到的:

迭代数组只获得Swift Playground中的第一个值

3 个解决方案

#1


7  

If you're using a playground, you might need to change the view in order to see all the results..

如果您正在使用游乐场,则可能需要更改视图才能查看所有结果。

You need to hover over the quick view display in order to find the "All Values" icon.

您需要将鼠标悬停在快速查看显示上才能找到“所有值”图标。

迭代数组只获得Swift Playground中的第一个值


It looks like this may have changed in Xcode 7 - as @MikeMeyers mentions in the comments, you need to right click on the quick view window and select the value history option.

看起来这可能在Xcode 7中发生了变化 - 正如@MikeMeyers在评论中提到的那样,您需要右键单击快速视图窗口并选择值历史记录选项。

迭代数组只获得Swift Playground中的第一个值

#2


0  

I can't reproduce what you're saying:

我不能重现你说的话:

rnapier:~$ swift
Welcome to Swift version 1.2. Type :help for assistance.
  1> var arrayA = ["Mike", "James", "Stacey", "Steve"]
  2. var str = String()
  3. for array in arrayA {
  4.     str = array
  5.     println(str)//Just prints steve
  6. }
Mike
James
Stacey
Steve
arrayA: [String] = 4 values {
  [0] = "Mike"
  [1] = "James"
  [2] = "Stacey"
  [3] = "Steve"
}
str: String = "Steve"

This prints all the values, and leaves str assigned to the last element. It's unclear here why you're using str at all, but I believe it does what you ask.

这将打印所有值,并将str分配给最后一个元素。目前还不清楚你为什么要使用str,但我相信它能满足你的要求。

If I were writing this loop, I would recommend:

如果我正在写这个循环,我会建议:

let arrayA = ["Mike", "James", "Stacey", "Steve"]
for elem in arrayA {
    println(elem)
}

#3


0  

Your code runs just fine - it iterates over all array elements, and it prints them all.

您的代码运行得很好 - 它遍历所有数组元素,并将它们全部打印出来。

I notice a bit of redundancy though - it can be rewritten more concisely as:

我注意到了一些冗余 - 它可以更简洁地重写为:

var arrayA = ["Mike", "James", "Stacey", "Steve"]
for element in arrayA {
    println(element)//Just prints steve
}

#1


7  

If you're using a playground, you might need to change the view in order to see all the results..

如果您正在使用游乐场,则可能需要更改视图才能查看所有结果。

You need to hover over the quick view display in order to find the "All Values" icon.

您需要将鼠标悬停在快速查看显示上才能找到“所有值”图标。

迭代数组只获得Swift Playground中的第一个值


It looks like this may have changed in Xcode 7 - as @MikeMeyers mentions in the comments, you need to right click on the quick view window and select the value history option.

看起来这可能在Xcode 7中发生了变化 - 正如@MikeMeyers在评论中提到的那样,您需要右键单击快速视图窗口并选择值历史记录选项。

迭代数组只获得Swift Playground中的第一个值

#2


0  

I can't reproduce what you're saying:

我不能重现你说的话:

rnapier:~$ swift
Welcome to Swift version 1.2. Type :help for assistance.
  1> var arrayA = ["Mike", "James", "Stacey", "Steve"]
  2. var str = String()
  3. for array in arrayA {
  4.     str = array
  5.     println(str)//Just prints steve
  6. }
Mike
James
Stacey
Steve
arrayA: [String] = 4 values {
  [0] = "Mike"
  [1] = "James"
  [2] = "Stacey"
  [3] = "Steve"
}
str: String = "Steve"

This prints all the values, and leaves str assigned to the last element. It's unclear here why you're using str at all, but I believe it does what you ask.

这将打印所有值,并将str分配给最后一个元素。目前还不清楚你为什么要使用str,但我相信它能满足你的要求。

If I were writing this loop, I would recommend:

如果我正在写这个循环,我会建议:

let arrayA = ["Mike", "James", "Stacey", "Steve"]
for elem in arrayA {
    println(elem)
}

#3


0  

Your code runs just fine - it iterates over all array elements, and it prints them all.

您的代码运行得很好 - 它遍历所有数组元素,并将它们全部打印出来。

I notice a bit of redundancy though - it can be rewritten more concisely as:

我注意到了一些冗余 - 它可以更简洁地重写为:

var arrayA = ["Mike", "James", "Stacey", "Steve"]
for element in arrayA {
    println(element)//Just prints steve
}