使用 arc4random_uniform in Swift 从数组中提取随机字符串
Pulling random string from array with arc4random_uniform in Swift
当我 运行 应用程序时,我不断收到错误消息:
"Could not cast value of type 'Swift._NSContiguousString' to 'NSArray'."
我也试过转换为 String
,但这显然也不是解决方案。有没有人遇到过这个?我只是想从数组中提取一个随机字符串。
firstArray = ["firstItem", "secondItem", "thirdItem"]
randomArray = firstArray[Int(arc4random_uniform(UInt32(firstArray.count)))] as! NSArray
谢谢
保持数组定义如下:
var firstArray = ["firstItem", "secondItem", "thirdItem"]
然后从这里做随机数:
let randomIndex = Int(arc4random_uniform(UInt32(firstArray.count)))
得到这样的结果:
var result = self.firstArray[randomIndex]
祝你好运!
当我 运行 应用程序时,我不断收到错误消息:
"Could not cast value of type 'Swift._NSContiguousString' to 'NSArray'."
我也试过转换为 String
,但这显然也不是解决方案。有没有人遇到过这个?我只是想从数组中提取一个随机字符串。
firstArray = ["firstItem", "secondItem", "thirdItem"]
randomArray = firstArray[Int(arc4random_uniform(UInt32(firstArray.count)))] as! NSArray
谢谢
保持数组定义如下:
var firstArray = ["firstItem", "secondItem", "thirdItem"]
然后从这里做随机数:
let randomIndex = Int(arc4random_uniform(UInt32(firstArray.count)))
得到这样的结果:
var result = self.firstArray[randomIndex]
祝你好运!