0

I am new to coding and am trying to grasp how classes work in Swift. I wrote the follow code in Xcode 7.2.1 and receive an error 'Expected declaration' which points at the first character in line myFirstCar.refuel().

    import UIKit

    class ViewController: UIViewController {

        class Car {

            var weight:Int = 1200
            var gas:Int = 100
            var maxGas:Int = 1000 

            func refuel() {
                if gas < 900 {
                    gas += 100
                } else {
                    gas += (maxGas - gas)
                }
         }
    }

    let myFirstCar = Car()

    myFirstCar.refuel()

}

I would like this code to run the refuel() function inside the class.

I have looked around and believe this is a problem with the initialization of the function inside the Car class. I have tried initializing each variable in the class as well as initialize the function but I receive the same error code pointing at the same line.

I now think that I might be missing some vary fundamental rule when working with classes.

For instance, I do not know if the class ViewController: UIViewController is an issue because my class Car is contained within it and might be making the Car class a subclass of ViewController?

Like I said, I have looked around and found this code online which is essentially the same as mine.

class Tank {
    class var bonusDamage: Double {
        return Double(Upgrade.level) * 2.5
    }

    let baseDamage = 10.0
    var damage: Double {
        return self.baseDamage + Tank.bonusDamage
    }

    class func upgrade() {
        Upgrade.level += 1
    }

    struct Upgrade {
        static var level = 0
    }
}

var tank = Tank()

println(tank.damage)
// 10.0

Tank.upgrade()

println(tank.damage)
// 12.5

Tank.upgrade()

println(tank.damage)
// 15.0

If I copy and paste the code into Xcode, it gives the same error 'Expected declaration' at the line:

println(tank.damage)
// 10.0

I guess my real problem is I do not understand this error. If someone could point me in a direction to look for an answer, that would be much appreciated!

1
  • The problem is that the Car class should not be nested inside the ViewController class. Ideally, move it out to its own file Car.swift. You also seem to be using an old version of Swift, since println has been deprecated. Commented Feb 23, 2016 at 23:44

1 Answer 1

1

The problem arises because you are calling a function in the wrong place:

myFirstCar.refuel()

Likewise for:

println(tank.damage)
// 10.0

Tank.upgrade()

println(tank.damage)
// 12.5

Tank.upgrade()

println(tank.damage)
// 15.0

One has to call this within in func scope; for instance:

override func viewDidLoad()
{
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    myFirstCar.refuel()

    /* 

    println(tank.damage)
    // 10.0

    Tank.upgrade()

    println(tank.damage)
    // 12.5

    Tank.upgrade()

    println(tank.damage)
    // 15.0
    */
}

By the way, if you are using Swift 2, println has been renamed to print.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.