Developingdeveloper
Developingdeveloper

Reputation: 19

Conditions combined with logic OR - Swift (error)

I`m following a tutorial from Ray Wenderlich
(http://www.raywenderlich.com/81952/intro-object-oriented-design-swift-part-1)

This function gives an error. The problem is the line with the logic OR operator ( || ). I can`t find the cause of the issue.

The function:

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

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

    return "Turn \(normalDegrees) degrees"
}

The error I get, says: Expected "," separator

The function is a part of a class, but i don´t see why that should be a problem. I´ll upload the code if that is necessary. I´m working with the code in a playground file.

The rest of the 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 ""
    }
}

Upvotes: 0

Views: 109

Answers (1)

Kametrixom
Kametrixom

Reputation: 14973

The condition in the if statement is really weird: When something should execute when either the first value is greater than the second or the second is greater than the first, then that's like checking whether the values are indifferent! So

if normalDegrees > degreesInACircle || normalDegrees < degreesInACircle

is equal to

if normalDegrees != degreesInACircle

Also you can convert

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

to

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

(Is exactly the same, just more concise)

There are some more weird things in your code. What I think you really want is really simple:

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

(Try it out)

Upvotes: 1

Related Questions