为什么这个代码会在swift playground中计算6个元素,如9("错误")。
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)。
答案 0 :(得分:0)
您要将新元素添加到同一 stringArray ,其中已包含内容 [&#34; 1&#34;,&#34; 2&#34;,&#34; 3& #34;,&#34; 4&#34;,&#34; 5&#34;,&#34; 6&#34;] 。然后将&#34;段落(i)&#34; 字符串附加到其中3次。所以,新内容现在是, [&#34; 1&#34;,&#34; 2&#34;,&#34; 3&#34;,&#34; 4&#34;,&# 34; 5&#34;,&#34; 6&#34;,&#34;段落1&#34;,&#34;段落2&#34;,&#34;段落3&#34;] 。多数计数如何达到9。