使用 Swift 语言从 xCode 获取编译错误消息:“extra argument userinfo in call”。问题是如何使用定时器中的 userInfo 数据到通知中心的参数“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 有时会给你一些奇怪的编译器错误,这相当于“有多个可能的选项,但没有一个可行,所以这是其中一个的问题所在”。当它提示的不是你想要的那个时,这可能会很奇怪
这就是为什么 Swift 如此频繁地告诉您某些东西“不是 Int8”,即使这与您尝试做的事情无关(嗯,我正在尝试连接两个字符串,Int8 必须做什么有什么?——这是因为 +
运算符的一个可能选项是在 Int8 上工作,所以这是它选择提示的那个)。
在这种情况下,postNotificationName
有多个重载版本,一个有 1 个参数,一个有 2 个,一个有 3 个(你想要的那个)。它们都不符合您提供的类型,所以它说“其中一个选项是一个带有 2 个参数的调用,而您提供了 3 个,所以那行不通,还有一个额外的参数”。
不幸的是,这真的很令人困惑,并且让您无法找出真正错误的地方。假设您剪切并粘贴了实际代码,看起来 MPMoviePlayerPlaybackDidFinishNotification
中存在拼写错误,并且 userInfo
参数标签后面缺少一个冒号。
(p.s. 你不需要在返回 void 的函数末尾显式地放置一个 return,尽管它没有任何坏处)
关于swift - 调用中的额外参数 userinfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27620493/