// Start the timer if it is not active already.
if (!blinkTimer)
blinkTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(blinkCursorPositionMarker)
userInfo:nil
repeats:YES];
if blinkTimer == nil {
blinkTimer = NSTimer(timeInterval: 0.5, target: self, selector: "blinkCursorPositionMarker:", userInfo: nil, repeats: true)
}
Turns out that when we create a timer ourselves, we also have to assign it to a run loop. Which is not all that difficult, that can be done as follows:
blinkTimer = NSTimer(timeInterval: 0.5, target: self, selector: "blinkCursorPositionMarker:", userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(blinkTimer, forMode: NSDefaultRunLoopMode)
But why go through that if the same factory function is available in Swift? The following solution shows the usage of the factory:
if blinkTimer == nil {
blinkTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "blinkCursorPositionMarker:", userInfo: nil, repeats: true)
}
This code is now the same as the Objective-C code, and the created timer is immediately assigned to the run-loop.
Lesson to self: be careful when replacing a factory function with an object instantiation!
Happy coding...
Did this help?, then please help out a small independent.
If you decide that you want to make a small donation, you can do so by clicking this
link: a cup of coffee ($2) or use the popup on the right hand side for different amounts.
Payments will be processed by PayPal, receiver will be sales at balancingrock dot nl
Bitcoins will be gladly accepted at: 1GacSREBxPy1yskLMc9de2nofNv2SNdwqH
We don't get the world we wish for... we get the world we pay for.
No comments:
Post a Comment