0

为什么此代码将 6 个元素计为 9 个(“错误”)在 swift 操场上。

var stringArray = ["1", "2", "3", "4", "5", "6"]

for var i = 0; i < 3; i++ {
    stringArray.append("Paragraph" + "\(i)")
}


func concat (array: [String]) -> String {
    let count = UInt32(stringArray.count)                      ** --> =9 **
    let randomNumberOne = Int(arc4random_uniform(count))
    let randomNumberTwo = Int(arc4random_uniform(count))
    let randomNumberThree = Int(arc4random_uniform(count))

    let concatString = array[randomNumberOne] + array[randomNumberTwo] + array[randomNumberThree]

    return concatString
}

let finalString = concat(stringArray)

...但将此代码计为 6(正确)

var stringArray = ["1", "2", "3", "4", "5", "6"]              ** --> =6 **

let count = UInt32(stringArray.count)

它与 64 与 32 位有关吗?我有 Xcode 版本 6.0 (6A313)。

4

1 回答 1

0

您正在将新元素附加到已经具有内容的相同stringArray ["1", "2", "3", "4", "5", "6"]。然后将“第 (i) 段”字符串附加 3 次。所以,现在的新内容是["1", "2", "3", "4", "5", "6", "Paragraph 1", "Paragraph 2", "Paragraph 3"]。就这样计数达到了9。

于 2014-09-20T14:37:08.513 回答