我使用arc4random_uniform()从给定的String或Int中获取随机值。现在我想加入那些价值观。
var randomNumber = arc4random_uniform(9999) + 1000 // returns random number between 1000 and 9999 e.g. - 7501
let alphabet_array = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
let randomIndex = Int(arc4random_uniform(UInt32(alphabet_array.count)))
let random_alphabet = alphabet_array[randomIndex] // returns random alphabet e.g. - E
我想要的是像7501E一样显示7501和E
var str="\(randomNumber)\(random_alphabet)" // error
我收到以下错误:
Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254
请帮助我如何一起显示。
答案 0 :(得分:0)
正确的标点符号可以正常工作:
var randomNumber = arc4random_uniform(999) + 1000
// returns random number between 1000 and 9999 e.g. - 7501 NOTE 999 not 9999
let alphabet_array = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
// Note missing "=" in OP
let randomIndex = Int(arc4random_uniform(UInt32(alphabet_array.count)))
let random_alphabet = alphabet_array[randomIndex] // returns random alphabet e.g. - E
var str = "\(randomNumber)\(random_alphabet)" // e.g. 1197A
答案 1 :(得分:0)
您也可以将随机数转换为字符串,然后将它们一起添加,而不是使用字符串插值:
let str = randomNumber.description + random_alphabet