Search code examples
swiftnstimer

does not implement methodSignatureForSelector: Unrecognized selector (NSTimer)


I am using an NSTimer in my Swift app in the following way:

func playEvent(eventIndex : Int){
    if (eventIndex < 2){
        let currEvent = self.eventArray[eventIndex]
        currEvent?.startEvent()
        let nextIndex = eventIndex + 1
        NSTimer.scheduledTimerWithTimeInterval((currEvent?.duration)!, target: self, selector: "playEvent(nextIndex)", userInfo: nil, repeats: true)
    }
    else if (eventIndex==2){
        self.eventArray[eventIndex]?.startEvent()
        NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: "finishSentence", userInfo: nil, repeats: true)
    }
    else{
        //do nothing
    }
}

I think the problem may be either that I am not property calling the selector method with a parameter. If that is impossible, how could I work around this? There is being called on the first NSTimer instance. Here is the full Swift class for reference: https://gist.github.com/ebbnormal/79941733b82f5fe58282


Solution

  • You can't pass nextIndex into a selector directly. One option for NSTimer could be to pass values via userInfo, for example:

    
    
        NSTimer.scheduledTimerWithTimeInterval(1, target: self, 
            selector: "mySelector:", 
            userInfo: NSNumber(integer: 999), 
            repeats: true)
    
        func mySelector(timer: NSTimer){
                print(timer.userInfo)
        }