Penard Corentin
Penard Corentin

Reputation: 47

Swift - Operators + Variables

I'm not able to do with swift : String1 = - String2 + 2

Actually :

var SetupTimer = "123"

var currentTime = 0

In an other fonction i do this :

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
    var itemSelected = food[row]
    itemLabel.text = itemSelected
    bb = itemSelected 
}

SetupTimer = bb

And finally i want to do that :

var currentTime = - SetupTimer + 10

Can someone help me?

Thank you

EDIT :

func CountDown()
{        
    if let timerAsInt = SetupTimer.toInt()
    {
        var currentTime = -timerAsInt + 10
    }

    currentTime = currentTime + 1

    self.lbltimer.text = (String(10 - currentTime))
}

Upvotes: 0

Views: 63

Answers (1)

Luca Angeletti
Luca Angeletti

Reputation: 59496

I am using the same preconditions of the answer by Craig Otis.

So, if you have a String setupTimer that does represent an integer

let setupTimer = "123"

then you can write:

Swift 2.0

if let timerAsInt = Int(setupTimer) {
    var currentTime = -timerAsInt + 10
}

Swift 1.2

if let timerAsInt = setupTimer.toInt() {
    var currentTime = -timerAsInt + 10
}

Hope this helps.

Update

If you want to update your own currentTime variable this is the code

if let timerAsInt = setupTimer.toInt() {
    currentTime = -timerAsInt + 10
}

Upvotes: 1

Related Questions