Unrecognized selector sent to class when using NSTimer in Swift; NSInvalidArgumentException - swift

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!

Related

Swift 5 - Timer not firing

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.

Swift timer method terminates the app early with SIGABRT error

Below is my code for having a timer change a variable from 0 to 1, which results in a SIGABRT error when the timer tries to fire:
var timerToggle = 0
let timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: false)
#objc func fireTimer() {
timerToggle = 1
}
I know others who've had this problem seem to solve it by placing the timer declaration within the override func viewDidLoad() function but whenever I do that it gives me the following error
"Initialization of immutable value 'timer' was never used; consider replacing with assignment to '_' or removing it."
Any help would be greatly appreciated
"Initialization of immutable value 'timer' was never used; consider
replacing with assignment to '_' or removing it."
This is just a warning not an error. You can still go ahead and initialise timer in viewDidLoad.
If it is needed to activate once-fired timer in viewDidLoad, it should look somehow like the following
class MyController: UIViewController {
var timerToggle = 0
private var timer: Timer?
deinit {
timer?.invalidate()
}
#objc func fireTimer() {
timerToggle = 1
timer = nil
}
override func viewDidLoad() {
super.viewDidLoad()
timer = Timer.scheduledTimer(timeInterval: 5.0, target: self,
selector: #selector(fireTimer), userInfo: nil, repeats: false)
}
}
If you are not going to access the timer anymore simply add below to viewDidLoad
_ = Timer.scheduledTimer(timeInterval: 5.0,
target: self,
selector: #selector(fireTimer),
userInfo: nil,
repeats: false)

instance error from unreconized selector with #objc [duplicate]

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

Timer on swift not working

I have a function working with a button:
#IBAction func btnSiguiente(_ sender: Any) {
if indexImagenes == imagenes.count {
indexImagenes = 0
}
escaparate.image = NSImage(named: imagenes[indexImagenes])
indexImagenes += 1
}
I want to make it work with a Timer:
var playTimer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(autoplay), userInfo: nil, repeats: true)
func autoplay() {
if indexImagenes == imagenes.count {
indexImagenes = 0
}
escaparate.image = NSImage(named: imagenes[indexImagenes])
indexImagenes += 1
}
But I get this in console:
[_SwiftValue autoplay]: unrecognized selector sent to instance 0x6080000451c0
To fix this, initialize the timer once the instance of the class your are in is fully initialized.
So just give the var playTimer a default value as follows:
var playerTimer:Timer? = nil
Then initialize the timer in some function that gets called once 'self' is fully initialized (like viewDidLoad() for example, assuming this code is in a viewController):
override func viewDidLoad()
{
super.viewDidLoad()
playerTimer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(autoplay), userInfo: nil, repeats: true)
}
Essentially just make sure 'self' is fully initialized before you send it a selector to perform.

How is that possible to post notification after 1 second delay or in a dispatch asyn?

i just want to call a method after loading my view. i have set notification to one view and postnotification to other view you will get more idea from my code.. here is my code..
in a one.swift file
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "InsertNote:", name: "insert", object: nil)
}
func InsertNote(notification:NSNotification)
{
println("blablalbla")
}
in a second.swift file
override func viewDidLoad() {
super.viewDidLoad()
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("getData"), userInfo: nil, repeats: false)
}
func getData()
{
NSNotificationCenter.defaultCenter().postNotificationName("insert", object: nil)
}
i have try also in a dispatch_asynch but still it is not work.
Try this
let delayInSeconds: Double = 1
override func viewDidLoad() {
super.viewDidLoad()
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))), dispatch_get_main_queue()){
NSNotificationCenter.defaultCenter().postNotificationName("insert", object: nil)
}
}
The only reason I can come up with that this doesn't work, is that your first ViewController A is already deallocated when you send the notification with the second ViewController B. So, you are sending the notification with B correctly, but A isn't around to receive it.
You can test this by adding an observer in B as well.
override func viewDidLoad() {
super.viewDidLoad()
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("getData"), userInfo: nil, repeats: false)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "insertNote:", name: "insert", object: nil)
}
func getData() {
NSNotificationCenter.defaultCenter().postNotificationName("insert", object: nil)
}
func insertNote:(note: NSNotification) {
print("Banana")
}
// Should print 'Banana' after one second.