Swift 调用中的额外参数userinfo

Swift 调用中的额外参数userinfo,swift,timer,notifications,userinfo,Swift,Timer,Notifications,Userinfo,使用Swift语言从xCode获取编译错误消息:“调用中的额外参数userinfo”。问题是如何将计时器中的userInfo数据用于notification center中的参数“userInfo” func onSetAudioWithTrackIndex(CurrentTrackIndex:Int, urlAudio: String){ ........ //try to pass currentTrackIndex data to timerfire pl

使用Swift语言从xCode获取编译错误消息:“调用中的额外参数userinfo”。问题是如何将计时器中的userInfo数据用于notification center中的参数“userInfo”

    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
}

Swift有时会给您带来一些奇怪的编译器错误,这相当于“有多个可能的选项,它们都不起作用,下面是其中一个的错误”。当它抱怨的不是你想要的,这可能会很奇怪

这就是为什么斯威夫特经常告诉你某些东西“不是Int8”,即使这与你试图做的事情无关(嗯,我试着连接两个字符串,Int8s和什么有什么关系?–这是因为
+
操作符的一个可能选项是处理Int8s,所以它选择抱怨这个)

在本例中,
postNotificationName
有多个重载版本,一个有1个参数,一个有2个,一个有3个(您想要的参数)。它们都不适合您提供的类型,所以它说“其中一个选项是一个有2个参数的调用,而您提供了3个,所以这不起作用,还有一个额外的参数”

不幸的是,这真的很令人困惑,让你感觉不到什么是真正的错误。假设你剪切并粘贴了实际的代码,看起来在
mpmovieplayerplaybackdidfishnotification
中有拼写错误,
userInfo
参数标签后面缺少一个冒号

(另外,您不需要在返回void的函数末尾显式地放置一个return,尽管它不会造成任何伤害)

问题是
NSTimer
userInfo
属性是可选的:

因此,
trackIndexDict
具有不可转换的类型
[String:AnyObject?]
postNotificationName()
的最后一个参数所预期的,发送到
[NSObject:AnyObject]

使用可选绑定,您可以“测试并展开”属性:

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

        NSNotificationCenter.defaultCenter().postNotificationName(MPMoviePlayerPlaybackDidFinishNotification,
            object: self,
            userInfo: trackIndexDict)
    }
}
谢谢,Martin,这(NSTimer的用户信息是可选的)是根本原因。通过以下更改。这是可以修复的

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

在swift 3中,我遇到了同样的错误。当我将swift 2.2转换为swift 3时,由于语法发生了更改,所以它抛出了这个错误

Swift 3:

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

你能检查一下你的标签是否正确和完整吗?例如,这是什么语言?第一次在这里提出问题。更新了描述和标签。语言很快。请复制/粘贴你的真实代码。拼写错误如
mpmoiveplayerplaybackdidfishnotification
userInfo[“trackIndex”:timer.userInfo]中缺少冒号
让你的问题变得不清楚。
中的感叹号
如果让timerUserInfo:AnyObject=timer.userInfo!
是错误的,代码不应该编译。除此之外,这不正是我建议的吗?(注意,你可以“接受”单击复选标记可以获得有用的答案。这会将问题标记为已解决,并为您和答案的作者提供一些声誉分数。)感谢空速,这为我指明了正确的方向。在我的例子中,我使用了一个带有字符串rawValues的枚举类型来定义不同的通知名称,但忘记了将其展开为字符串:NSNotificationCenter.defaultCenter().postNotificationName(Notifications.MyNote1,object:nil,userInfo:[“myKey”:myValue]),这给了我额外的参数错误。在我将enum选项展开为类似Notifications.MyNote1.rawValue的rawValue后,编译错误消失。
NotificationCenter.default.post(name: NSNotification.Name(rawValue: MPMoviePlayerPlaybackDidFinishNotification), object: self, userInfo: trackIndexDict)