extra argument userinfo in call

China☆狼群 提交于 2019-12-23 15:13:25

问题


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
}

回答1:


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)




回答2:


In swift 3 , I got this same error. When I was converting the 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)



回答3:


The problem is that the userInfo property of NSTimer is an optional:

var userInfo: AnyObject? { get }

so that trackIndexDict has the type [String : AnyObject?] which is not convertible to [NSObject : AnyObject] as expected by the last parameter of postNotificationName().

With optional binding you can "test and unwrap" the property:

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

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



回答4:


thanks, Martin, this(userInfo of NSTimer is optional) is root cause. with the below change. this can be fixed

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


来源:https://stackoverflow.com/questions/27620493/extra-argument-userinfo-in-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!