0

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”)
 }


}

1 Answers1

0

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

MCMatan
  • 8,623
  • 6
  • 46
  • 85
  • when i use ur way, it starts the timer at the start of my application automaticly when i actualy want to start later via a function – bibble triple Mar 24 '19 at 18:47
  • That is correct. If you would like it to run at a different time, simply call it from a different place (Not from init) or make time interval longer – MCMatan Mar 24 '19 at 18:48
  • makes sense, its how i had done it originaly but i realy wanted to combine the specification of the timer in its main variable as seen in my example is there a way to do that at all? – bibble triple Mar 24 '19 at 20:44
  • Not sure for what purpose would it be good, is there a specific time you would like to to execute? As a class specification there is no option – MCMatan Mar 24 '19 at 22:05
  • just to simplify the code – bibble triple Mar 24 '19 at 23:14