gpt4 book ai didi

swift - for 循环不会从数组/ Playground 中传递多次相同的条目,为什么?

转载 作者:行者123 更新时间:2023-11-30 14:17:58 24 4
gpt4 key购买 nike

我想问,我是否错了,或者这是“正常”行为。将以下代码放入类中并通过单击按钮调用它时,它会多次提供相同的输出。但将其放入 Playground 时,它只会向我提供一次输出。

它属于这段代码:

class fruits {

var fruitID: String!
var fruitName: [String]!
var fruitColor: [String]!

init(fruitID: String, fruitName: [String], fruitColor: [String]) {
self.fruitID = fruitID
self.fruitName = fruitName
self.fruitColor = fruitColor
}

}

--> 代码

var i_fruitID = "AAA"
var i_fruits = ["Apple", "Banana"]
var i_fruitColor = ["red", "yellow"]

var myFruit = fruits(fruitID: i_fruitID, fruitName: i_fruits, fruitColor: i_fruitColor)
var myFruits = [fruits]()

myFruits.append(myFruit)

for fruit in myFruits {

println("++ FRUITID ++ : \(fruit.fruitID)")

for fruitName in fruit.fruitName {
println("++ FRUITNAME ++ : \(fruit.fruitName)")
//break
}

for fruitColor in fruit.fruitColor {
println("++ FRUITCOLOR ++ : \(fruit.fruitColor)")
//break
}
}

<-- 代码

结果:

**in CODE:**

++ FRUITID ++ : AAA
++ FRUITNAME ++ : [Apple, Banana]
++ FRUITNAME ++ : [Apple, Banana]
++ FRUITCOLOR ++ : [red, yellow]
++ FRUITCOLOR ++ : [red, yellow]


**in PLAYGROUND:**

++ FRUITID ++ : AAA
++ FRUITNAME ++ : [Apple, Banana]
++ FRUITCOLOR ++ : [red, yellow]

将 BREAK 放入代码中时,它只打印一次,就像在 Playground 上一样。现在这是怎么回事?

最佳答案

感谢@Kametrixom,解决方案如下:

for fruit in myFruits {

println("++ FRUITID ++ : \(fruit.fruitID)")

for fruitName in fruit.fruitName {
println("++ FRUITNAME ++ : \(fruitName)")
//break
}

for fruitColor in fruit.fruitColor {
println("++ FRUITCOLOR ++ : \(fruitColor)")
//break
}
}

关于swift - for 循环不会从数组/ Playground 中传递多次相同的条目,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30901799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com