user3915477
user3915477

Reputation: 275

Xcode NSTimer error: Unrecognized selector sent to class

I have a file called Util.swift with the following

class foo: NSObject {

    func sayHello() {
        print("hello")
    }
}

var globalTimer = NSTimer()

func startTheTimer() {
    globalTimer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: foo.self, selector: Selector("sayHello"), userInfo: nil, repeats: true)
}

When I call startTheTimer() in the viewDidLoad of any of my viewControllers, I get an error saying "unrecognized selector sent to class". I don't know what I have done wrong here.

Upvotes: 2

Views: 417

Answers (2)

Abdul Yasin
Abdul Yasin

Reputation: 3508

I have updated your code here:

class foo: NSObject {

class func sayHello() {
    print("hello")
}
}

var globalTimer = NSTimer()
func startTheTimer() {
globalTimer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: foo.self, selector: Selector("sayHello"), userInfo: nil, repeats: true)
}

Upvotes: 0

MudOnTire
MudOnTire

Reputation: 506

class func sayHello() {
    print("hello")
}

make this a class method please.

Upvotes: 2

Related Questions