NSTimer Not working swift

I am using an nstimer to count down but it seems to be counting down in 4s instead of 1s here is my code can you help me fixs this:



func failsafeon() {

if failsafeper == "off" {

if currentamountofwrong == 0 {

currentamountofwrong = currentamountofwrong + 1

count1 = 20

timer1 = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("countdown"), userInfo: nil, repeats: true)

}

if currentamountofwrong == 1 {

currentamountofwrong = currentamountofwrong + 1

count1 = 45

timer1 = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("countdown"), userInfo: nil, repeats: true)

}

if currentamountofwrong == 2 {

currentamountofwrong = currentamountofwrong + 1

count1 = 90

timer1 = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("countdown"), userInfo: nil, repeats: true)

}

if currentamountofwrong > 2 {

currentamountofwrong = currentamountofwrong + 1

count1 = 90

timer1 = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("countdown"), userInfo: nil, repeats: true)

}

a.enabled = false

b.enabled = false

c.enabled = false

d.enabled = false

a.alpha = 3

b.alpha = 3

c.alpha = 3

d.alpha = 3

} else {

print("failsafeoff")

}

}

func countdown() {

print("count down start1")

faillabel()

--count1

faillabel()

if count1 == 0 || count1 < 1 {

timer1.invalidate()

count1 = 0

faillbl.text = "FailSafe: OFF"

a.enabled = true

b.enabled = true

c.enabled = true

d.enabled = true

a.backgroundColor = UIColor.whiteColor()

b.backgroundColor = UIColor.whiteColor()

c.backgroundColor = UIColor.whiteColor()

d.backgroundColor = UIColor.whiteColor()

a.alpha = 10

b.alpha = 10

c.alpha = 10

d.alpha = 10

}

}

func faillabel() {

faillbl.text = "FailSafe: \(count1) Remaning"

print("count down start2")

}

Accepted Reply

You're incrementing currentamountofwrong in each if block. So the next one always matches. Use "else if" instead of "if" so that only one will match.


On a side note. You need to learn how to use the debugger. Set a breakpoint, step through it line by line, look at your variables and see where the code goes.

Replies

You're incrementing currentamountofwrong in each if block. So the next one always matches. Use "else if" instead of "if" so that only one will match.


On a side note. You need to learn how to use the debugger. Set a breakpoint, step through it line by line, look at your variables and see where the code goes.

Just nitpicking: shouldn't the selector look like


func countdown( timer:NSTimer ) ?