Additional userinfo argument in the call

Get compilation error message from xCode with Swift language: "additional userinfo argument in call". the question is how to use the userInfo data from the timer to the "userInfo" argument in the action 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
}

      

+3


source to share


4 answers


Swift can sometimes give you strange compiler errors, which means "there are several possible options, none of them work, so anyway with one of them." When he complains that this is not what you were trying to do, it can be very strange.

This is why Swift often tells you that something is "not Int8" even though it has nothing to do with what you were trying to do (yes, I was trying to concatenate two strings, what was Int8s supposed to do with anything? - that one of the possible options for the operator +

is to work with Int8s, so that's the one he decides to complain about).

In this case, it postNotificationName

has multiple overloads, one with 1 argument, one with 2, and one with 3 (the one you want). None of them match the types you supplied, so his saying "one of the options is a call with two arguments, and you supplied 3, so it won't work, it's an extra argument."



Unfortunately, this is really quite confusing and takes away the smell of what's actually wrong. Assuming you cut and pasted your actual code, it looks like a spelling error in MPMoviePlayerPlaybackDidFinishNotification

, and the argument label userInfo

doesn't have a colon after it.

(ps you don't need to explicitly put the return at the end of void-returning functions, although it doesn't do any harm)

+4


source


In swift 3, I got the same error. When I converted swift 2.2 to swift 3 as the syntax was changed, so it throws this error.

Swift 3:



NotificationCenter.default.post(name: NSNotification.Name(rawValue: MPMoviePlayerPlaybackDidFinishNotification), object: self, userInfo: trackIndexDict)

      

+2


source


The problem is that the property userInfo

NSTimer

is optional:

var userInfo: AnyObject? { get }

      

so it trackIndexDict

has a type [String : AnyObject?]

that is not convertible until [NSObject : AnyObject]

, as expected, the last parameter postNotificationName()

.

With optional binding, you can "check and expand" the property:

if currentTime == playDuration {
    if let timerInfo: AnyObject = timer.userInfo {
        let trackIndexDict = ["trackIndex" : timerInfo]

        NSNotificationCenter.defaultCenter().postNotificationName(MPMoviePlayerPlaybackDidFinishNotification,
            object: self,
            userInfo: trackIndexDict)
    }
}

      

+1


source


thanks Martin, this (userInfo from NSTimer is optional) is the main reason. with subsequent change. it can be fixed.

if let timerUserInfo: AnyObject = timer.userInfo! {      
      NotificationCenter.default.post(name: NSNotification.Name(rawValue: MPMoviePlayerPlaybackDidFinishNotification), object: self, userInfo: ["trackIndex":timerUserInfo])
}

      

+1


source







All Articles