This question already has answers here:
How do I run Asynchronous callbacks in Playground
(8 answers)
Closed 7 years ago.
I'd like to know how to use an NSTimer inside a Swift Playground. This question has been asked before, but none of the answers actually answered the question.
Here's my Playground code:
import Foundation
class MyClass {
func startTimer() {
NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "onTimer:", userInfo: nil, repeats: true)
}
func onTimer(timer:NSTimer!) {
println("Timer here")
}
}
var anInstance = MyClass()
anInstance.startTimer()
Instantiating NSTimer with scheduledTimerWithTimeInterval creates the timer and schedules it on the current run loop.
But the method onTimer is never called. Why?
First of all, your onTimer method have to be declared as #objc, or NSTimer cannot find that.
As for your question, it's because you haven't started the run loop.
To do that, CFRunLoopRun() is the simplest solution I think.
import Foundation
class MyClass {
func startTimer() {
NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "onTimer:", userInfo: nil, repeats: true)
}
#objc func onTimer(timer:NSTimer!) {
println("Timer here")
}
}
var anInstance = MyClass()
anInstance.startTimer()
CFRunLoopRun() // <-- HERE!
For the sake of completeness, as #MartinR metioned in the comment, you can also use XCPSetExecutionShouldContinueIndefinitely()
import Foundation
import XCPlayground
XCPSetExecutionShouldContinueIndefinitely()
class MyClass {
func startTimer() {
NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "onTimer:", userInfo: nil, repeats: true)
}
#objc func onTimer(timer:NSTimer!) {
println("Timer here")
}
}
var anInstance = MyClass()
anInstance.startTimer()
In this case the Playground runs only seconds specified in the Timeline:
Related
I have the following,
class someViewController : ViewController{
var timer:Timer? = nil
override func viewDidLoad() {
timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(fire), userInfo: nil, repeats: true)
}
#objc func fire(){
print("fired")
}
}
The fire function is never hit and I can't figure out why, what am I missing?
Try to clean Xcode or rebuild project, because your code works fine.
This question already has answers here:
Swift 4 Timer Crashes with NSException
(3 answers)
Timer on swift not working
(1 answer)
Closed 3 years ago.
when i run my aplication i get this error
Nick[66720:3658939] -[_SwiftValue function1]: unrecognized selector sent to instance
i tried moving out of the class and it says
only protocol member can be there
import Cocoa
class mymainclass: NSObject, NSApplicationDelegate{
var mytimer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(function1), userInfo: nil, repeats: true)
func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {
return dockMenu
}
#objc func function1(){
print(“function1 test”)
}
}
You are trying to use self on the class level, this way it is not referring to your created class instance.
Instead, you can create the timer after your class instance is created.
Inside the class constructor, there self will be referring to what you are expecting
class Mymainclass: NSObject, NSApplicationDelegate{
var mytimer: Timer?
override init() {
super.init()
self.mytimer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(function1), userInfo: nil, repeats: true)
}
#objc func function1(){
print("function1 test")
}
}
Note: Class names start with Capital letter
I copied the following code from Github in order to get more experience with playground but I keep getting an error
Type 'MyClass" has no Member on "onTimer'!
I tried to look at previous messages to find a solution, unfortunately, I couldn't. If possible can you please help me to solve it. Thanks
import UIKit
class MyClass {
func startTimer() {
Timer.scheduledTimerWithTimeInterval(2, target: self, selector: #selector(MyClass.onTimer(_:)), userInfo: nil, repeats: false)
}
#objc func onTimer(timer:Timer!) {
print("Timer here")
}
}
var anInstance = MyClass()
anInstance.startTimer()
CFRunLoopRun()
Change to ( Swift 4.2 )
Timer.scheduledTimer(timeInterval:2, target: self, selector: #selector(onTimer(_:)), userInfo: nil, repeats: true)
#objc func onTimer(_ timer:Timer)
Verify also if you need repeats: true) instead of repeats: false)
I'm trying to use an NSTimer to run a function at a certain interval, but every time it runs the program crashes with an exception:
+[Project.Class updateLabel]: unrecognized selector sent to class...
Terminating app due to uncaught exception "NSInvalidArgumentException".
The code is as follows:
class SecondViewController: UIViewController {
// MARK: Properties
override func viewDidLoad() {
super.viewDidLoad()
do {
try updateLabel()
try startTimer()
}
catch{
self.showAlert()
}
}
#IBOutlet weak var i2cLabel: UILabel!
// MARK: Actions
func startTimer() throws {
_ = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateLabel"), userInfo: nil, repeats: true)
}
func showAlert() {
}
func updateLabel() throws {
}
}
If I comment out the NSTimer line, the program compiles and runs just fine.
Note that updateLabel(), the selector function, takes no arguments so calling it as a selector without a colon suffix should work. This seems to be unique to this problem.
Try update your code to:
override func viewDidLoad() {
super.viewDidLoad()
do {
updateLabel()
try startTimer()
}
catch{
self.showAlert()
}
}
// MARK: Actions
func startTimer() throws {
_ = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateLabel"), userInfo: nil, repeats: true)
}
func showAlert() {
}
func updateLabel() {
}
It seems that: NSTimer don't know call a function throws. So it will crash.
I have tried to find some official or some article describe about it. But sad that I can't find now.
You can reference to this question: Reference
Hope this help!
I am trying to set scheduledTimerWithTimeInterval inside a struct but I keep getting the error Cannot invoke scheduledTimerWithTimeInterval with argument list of type. When I change the struct to a class it works fine.
My question is it possible to set a struct as the target of a NSTimer? The API says it should be of type AnyObject
struct MyTimer{
init() {
let timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timer"), userInfo: nil, repeats: true)
}
}
and
class MyTimer{
init() {
let timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timer"), userInfo: nil, repeats: true)
}
}
EDIT: It seems that for a class to be the target of a scheduledTimer it has to inherit from NSObject first. A struct could never to that.