Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extra argument userinfo in call

Get the compile error message from xCode with Swift language: "extra argument userinfo in call". the question is how to use userInfo data from timer to argument "userInfo" in notification center.

    func onSetAudioWithTrackIndex(CurrentTrackIndex:Int, urlAudio: String){

    ........
    //try to pass currentTrackIndex data to timerfire
    playTimeClock = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: "onSetPlayProgressTime:", userInfo: CurrentTrackIndex, repeats: true)

}

//Timerfire
func onSetPlayProgressTime(timer: NSTimer){

    var currentTime = mediaPlayer.currentPlaybackTime
    var playDuration = mediaPlayer.duration

    var trackIndexDict = ["trackIndex" : timer.userInfo]

    ................

    if currentTime == playDuration{                   
            NSNotificationCenter.defaultCenter().postNotificationName(MPMoviePlayerPlaybackDidFinishNotification, object: self, userInfo: trackIndexDict)
    }


    return
}
like image 914
lotosn Avatar asked Jan 10 '23 05:01

lotosn


1 Answers

Swift can give you slightly strange compiler errors sometimes, which amount to “there were multiple possible options, none of them worked, so here’s what’s wrong with one of them”. When the one it complains about is not the one you were trying for, this can be very strange

This is why Swift tells you so often that something is “not a Int8” even though that has nothing to do with what you were trying to do (huh, I was trying to concatenate two strings, what have Int8s got to do with anything? – it’s because one of the possible options for the + operator is to work on Int8s so that’s the one it chooses to complain about).

In this case, postNotificationName has multiple overloaded versions, one with 1 argument, one with 2, and one with 3 (the one you want). None of them fit the types you’ve supplied so it’s saying “one of the options is a call with 2 arguments, and you supplied 3, so that won’t work, there’s an extra argument”.

Unfortunately this is really quite confusing and throws you off the scent of what is actually wrong. Assuming you cut and pasted your actual code, looks like there’s a spelling error in MPMoviePlayerPlaybackDidFinishNotification, and the userInfo argument label is missing a colon after it.

(p.s. you don’t need to explicitly put a return at the end of functions that return void, though it doesn’t do any harm)

like image 57
Airspeed Velocity Avatar answered Jan 20 '23 07:01

Airspeed Velocity