Skip to main content
 首页 » 操作系统

ios之Swift 无法使用类型为 '==' 的参数调用 '(Int @lvalue [Int])'

2024年01月01日47sharpest

我真的是编程新手(所以这可能是一个非常愚蠢的问题),但我已经尝试按照 udemy.com 教程学习如何构建 iOS 应用程序。但是即使我一步一步地跟着它,它也不会让我编译。如果有人愿意花时间帮助我,我将不胜感激 =)

    func getSixRandom () -> String { 
    var lottoBalls = [Int]() 
    var result = "" 
    var n = [Int(arc4random() % 49) + 1] 
 
    lottoBalls += n 
 
    while lottoBalls.count < 6 { 
        n = [Int(arc4random() & 49) + 1] 
        var found = false 
 
    for ball in lottoBalls { 
    // Throws an error!! 
 
            if ball == n { 
                found = true 
            } 
        } 
 
        if found == false { 
            lottoBalls += n 
        } 
 
    } 
 
    result = "\(lottoBalls[0]), \(lottoBalls[1]), \(lottoBalls[2]), \(lottoBalls[3]), \(lottoBalls[4]), \(lottoBalls[5]) " 
 
    return result 
} 

请您参考如下方法:

这是您在比较 Int 和 [Int] 时应该期待的结果。

您需要更改以下行:

var n = [Int(arc4random() & 49) + 1] 

到:

var n = Int(arc4random() & 49) + 1 

并在您设置 n 的任何其他位置执行相同的操作。