Reference a variable value inside an @IBAction function from another class in Swift

464 views Asked by At

I'm trying to make a laundry timer app in Swift where the washer and drying will have different starting times, counting down to 0.

func updateTime() {
    ...
    var elapsedTime: NSTimeInterval = operationDuration - (currentTime - startTime)
    ...
   }

In the var elapsedTime I have operationDuration (how long the washer or dryer will take) and later on in the @IBAction func for pressing the "washer" button I have

let operationDuration == 1800

However I am getting an error the the updateTime func that

'operationDuration' is not defined.

How can I do this? Thanks in advance.

edit:

Here is my washerButtonPress code:

@IBAction func washerButtonPress(sender: AnyObject) {

        // TODO: start 30 minute countdown

        if !timer.valid {

            var operationDuration = 1800

            let aSelector:Selector = "updateTime"
            timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target:    self, selector: aSelector, userInfo: nil, repeats: true)
            startTime = NSDate.timeIntervalSinceReferenceDate()
        }

And how I'm trying to call it in my func updateTime()

var elapsedTime: NSTimeInterval = washerButtonPress.operationDuration - (currentTime - startTime)

and it returns '(AnyObject) -> ()' does not have a member named 'operationDuration'

I apologize for not knowing much but I'm pretty new to this

1

There are 1 answers

2
keithbhunter On

If this is merely a constant, you can declare it as a global variable.

let kOperationDuration = 1800

class Washer {
    ...

    @IBAction func washerButtonPress(sender: AnyObject) {
        if !timer.valid {
            let aSelector:Selector = "updateTime"
            timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target:    self, selector: aSelector, userInfo: nil, repeats: true)
            startTime = NSDate.timeIntervalSinceReferenceDate()
        }
    }

    ...
}

Then in the Dryer class you can reference the constant.

class Dryer {
    ...

    func updateTime() {
        ...
        var elapsedTime: NSTimeInterval = kOperationDuration - (currentTime - startTime)
        ...
    }

    ...
}