Nstimer Does Not Invoke a Private Func as Selector

NSTimer does not invoke a private func as selector

According to Using Swift with Cocoa and Objective-C:

“ Declarations marked with the private modifier do not appear in the generated header. Private declarations are not exposed to Objective-C unless they are explicitly marked with @IBAction, @IBOutlet, or @objc as well.”

Excerpt From: Apple Inc. “Using Swift with Cocoa and Objective-C (Swift 2 Prerelease).” iBooks. https://itun.es/us/utTW7.l

So, you can mark your function with @objc to get the desired behavior. I just tested this in one of my apps where I had used public visibility because I assumed that Objective-C couldn't see private declarations at all, and it works when marked as private and decorated with @objc.


I just saw this related question: Swift access control with target selectors — basically the same thing but I think yours is phrased in a more general way so not strictly a duplicate.

NSTimer don't call the method in selector

You need to schedule the timer on a run loop - you're just assigning it to a variable. Try using the class method + scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: -

so

NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "UpdateDeviceData:", userInfo: nil, repeats: true)

How to call private class function from selector in Swift.?

Adding NSObject when class declaration solved my problem.

Ref:NSTimer scheduledTimerWithTimeInterval and target is "class level function"

class MyClass:NSObject{}

and calling method as,

NSTimer.scheduledTimerWithTimeInterval(0.5, target: ClassName.self, selector: Selector("startAnimation"), userInfo: nil, repeats: true)

class func startAnimation(){}

NSTimer scheduledTimerWithTimeInterval - not calling funciton

You never actually start the timer because your startTimer() function will always return before reaching the line of code where you create the timer.

In your guard statement you only continue the execution of the function if internalTimer != nil but the only place where you set the timer is after that statement. Thus, your timer is never created and internalTimer will always be nil.

This should fix your problem:

func startTimer() {
guard internalTimer == nil else {
return print("timer already started")
}
internalTimer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: #selector(TimerService.timeEnded), userInfo: nil, repeats: false)
}

NSTimer timerWithTimeInterval not calling selector (Swift)

Replying to myself, I have found the problem. In the previous ViewController I am performing a segue after doing a http connection. In order to make it work, I have embedded the performSegue inside the dispatch:

dispatch_async(dispatch_get_main_queue(), {
self.performSegueWithIdentifier("goMySecondController", sender: nil)
})

In that second Controller is where I have the Timer that was not working. However, after this change is working properly.

Thanks for replies!

Swift nstimer accessing function other class

You want NSTimer to invoke func updateUILoop on the values object, so the target for timer's callback should be values, not self:

let timer = NSTimer(fireDate: NSDate(), interval: 5, target: self.values, selector: #selector(VariClass.updateUILoop), userInfo: nil, repeats: true)


Related Topics



Leave a reply



Submit