swift 编译错误:类型'Int'没有成员'random'

g52tjvyc  于 5个月前  发布在  Swift
关注(0)|答案(1)|浏览(74)
import Foundation

class Generator {
    func generate(_ count: Int) -> [Int] {
        var result = [Int]()
        
        for _ in 1...count {
            result.append(1 + Int.random(in: 1..<10))
        }

        return result
    }
}

字符串
我得到这个错误:

Compile Swift Module 'Base' (1 sources)
/eval/Sources/Base/Exercise.swift:10:31: error: type 'Int' has no member 'random'
            result.append(1 + Int.random(in: 1..<10))
                              ^~~ ~~~~~~

kx1ctssn

kx1ctssn1#

测试系统可能使用了过时的Swift版本,该版本不包含Int中的random成员。
在这种情况下,你可以恢复到Swift中生成随机数的旧方法之一,例如:

Int(arc4random() % 10) // This has same result as `Int.random(in: 0..<10)`

字符串

相关问题