条件结合逻辑或 - Swift(错误)

Conditions combined with logic OR - Swift (error)

我正在学习 Ray Wenderlich 的教程
(http://www.raywenderlich.com/81952/intro-object-oriented-design-swift-part-1)

这个函数报错。问题是逻辑 OR 运算符 ( || ) 的行。我找不到问题的原因。

函数:

func turn (degrees: Int) -> String {
    var normalDegrees = degrees
    let degreesInACircle = 360

    if (normalDegrees > degreesInACircle || normalDegrees < -degreesInACircle) {
        normalDegrees = normalDegrees % degreesInACircle
    }

    return "Turn \(normalDegrees) degrees"
}

我得到的错误是:Expected "," separator

该函数是 class 的一部分,但我不明白为什么这会成为问题。如果有必要,我会上传代码。我正在使用 playground 文件中的代码。

剩下的class:

class Vehicle {
    var brandName = ""
    var modelName = ""
    let modelYear = 0
    var powerSource = ""
    var numberOfWheels = 0

    func goForward() -> String {
        return ""
    }

    func goBackwards() -> String {
        return ""
    }

    func stopMoving() -> String {
        return ""
    }

    func turn (degrees: Int) -> String {
        var normalDegrees = degrees
        let degreesInACircle = 360

        if normalDegrees > degreesInACircle || normalDegrees < -degreesInACircle {
            normalDegrees = normalDegrees % degreesInACircle
        }

         return "Turn \(normalDegrees) degrees"
    }

    func changeGears (newGearName: String) -> String {
        return "Put \(modelName) into \(newGearName) gear"
    }

    func makeNoise () -> String {
        return ""
    }
}

if语句中的条件真的很奇怪:当第一个值大于第二个值或第二个值大于第一个值时应该执行某些操作时,这就像检查值是否无关紧要!所以

if normalDegrees > degreesInACircle || normalDegrees < degreesInACircle

等于

if normalDegrees != degreesInACircle

你也可以转换

func turn (degrees: Int) -> String {
    var normalDegrees = degrees

func turn (var normalDegrees: Int) -> String {

(完全一样,只是更简洁)

您的代码中还有一些更奇怪的东西。我想你真正想要的很简单:

func turn(degrees: Int) -> String {
    return "Turn \(degrees % 360) degrees"
}

(试一试)