objectiveccoder001
objectiveccoder001

Reputation: 3031

for 1 second: objective c coding help

Is it possible to do something like this?

if (myVariable == 100 for more than 1 second) {
}

So if myVariable equals 100 for more than 1 second do something.

Thanks! Elijah

Upvotes: 0

Views: 170

Answers (3)

James
James

Reputation: 5820

You could start an NSTimer from within the body of the if() statement that has a timer of 1 second. So something like

if(myVariable >= 100 && myTimer == nil){
    //NSTimer code here
}else if(myVariable < 100 && myTimer !=nil){
    //Invalidate timer
    //Set myTimer to nil
}

If you get the timer to call a method that then does what you want.

This should work but I can't help but feel there is a better solution to the actual problem your trying to solve.

Upvotes: 0

Eiko
Eiko

Reputation: 25632

You can store the time when you set myVariable to a new value (easy if you use a setter method for this all over the place). Then start a timer that fires after a second and check if you didn't change the value in meantime.

It's an unusual pattern though, and there are probably better ways to do what you try to accomplish.

Upvotes: 2

Klark
Klark

Reputation: 8280

I guess something like that is not possible. Different approach probably could solve that. i.e. you can leave some kind of a timestamp every time when someone changes myVariable and then just see how much has passed since the last change.

Upvotes: 1

Related Questions