Swift 4 Timer Crashes with Nsexception

Swift 4 Timer Crashes with NSException

As Andrea said, you should instantiate the timer in viewDidLoad. Also the documentation says:

The selector should have the following signature: timerFireMethod: (including a colon to indicate that the method takes an argument).

And don't forget to disable this timer in, for example, viewDidDisappear. You can't invalidate it in deinit because the repeating timer keeps a strong reference to its target, and your deinit will not get called as long as the timer is running. And if you remove it in viewDidDisappear, you might want to create the timer in viewDidAppear.

Thus, resulting in something like:

class ViewController: UIViewController {

weak var timer: Timer?

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(tick(_:)), userInfo: nil, repeats: true)
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)

timer?.invalidate()
}

@objc func tick(_ timer: Timer) {
print("tick")
}

}

Or you can use the block-based rendition, with [weak self] pattern, and the timer won't keep a strong reference to the view controller, in which case you can use deinit:

class ViewController: UIViewController {

var timer: Timer?

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

timer = Timer.scheduledTimer(withTimeInterval: 10, repeats: true) { [weak self] timer in // the `[weak self] reference is only needed if you reference `self` in the closure
print("tick")
}
}

deinit {
timer?.invalidate()
}

}

Terminating With Uncaught Exception Of Type NSException Timer Swift Crash

You are passing the wrong target to the timer. You want to pass clickClass, not self. And the selector should not reference the variable, it should reference the class.

timer = Timer.scheduledTimer(timeInterval: 1, target: clickClass, selector: #selector(playSound.repeatSound), userInfo: nil, repeats: true)

You should also take care to name things properly. Class, struct, and enum names should start with uppercase letters. Variable, function, and case names should start with lowercase letters.

Why is this NSTimer crashing?

The most significant information in the crash log is

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Digiverse.SimulatorViewController paintUniverse]: unrecognized selector sent to instance 0x7f91c504c6c0'

The selector paintUniverse in the timer declaration line expects a method func paintUniverse() on the top level of the class without a parameter.

Your function paintUniverse has three parameters which cannot be used as a timer selector anyway.

A timer selector can have no parameters at all:

selector: "paintUniverse" -> func paintUniverse()

or one single parameter which must be a NSTimerinstance and the selector must have a trailing colon:

selector: "paintUniverse:" -> func paintUniverse(timer : NSTimer)

Swift NSTimer makes app crash

Change the selector to timeListener: (note the colon).

var timer = NSTimer.scheduledTimerWithTimeInterval(0.5,
target: self, selector: "timeListener:", userInfo: data, repeats: true)

This is because in Objective-C,

func timeListener(timer:NSTimer)

would be written as

-(void)timeListener:(NSTimer*)timer

You need to add a colon everywhere the function wants an argument.

Swift Project Terminating with uncaught exception of type NSException

in RegisterPageViewController you were created the registerButtonTapped button action, but button method not implemented, if you are not used the registerButtonTapped delete from attribute inspector, else implenent the button action on class like the following

func registerButtonTapped(sender: UIButton)
{
}

instance error from unreconized selector with @objc

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

Swift Stopwatch App crashing, don't know where I'm going wrong

After looking at the console log, I am pretty sure the crash occurs because of a missing outlet connection.

Press Cmd+Shift+F and type 'play' in the search field.
Look for a entry in your storyboard file, of the form ...: Outlet = "play".

Click on that entry and remove it by clicking on the 'x' button next to play.

This should fix the crash.

Whenever you see an error of the form:
... this class is not key value coding-compliant for the key ...
you should check if there is a missing outlet in your storyboard or xib.



Related Topics



Leave a reply



Submit