我有一段代码如下:
extension Array {
fileprivate func random() -> Element {
let idx: Int
#if os(Linux)
idx = Int(random() % count)
#else
idx = Int(arc4random_uniform(UInt32(count)))
#endif
return self[idx - 1]
}
}
当我尝试部署我的应用程序时,在使用 linux 运行 docker 时出错,问题如下:
error: binary operator '%' cannot be applied to operands of type 'Int32' and 'Int' idx = Int(rand() % count)
我将问题追溯到这个函数,但无法在此处解决,因为它已经打算为 Linux 提供替代处理...
最佳答案
错误表明 random()
返回 Int32
。所以你需要转换一侧。
idx = Int(random()) % count
关于swift - 在 Linux 上使用 Swift 生成随机数时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56655883/