lot*_*osn 4 notifications timer userinfo swift
使用Swift语言从xCode获取编译错误消息:"调用中的额外参数userinfo".问题是如何使用来自计时器的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
}
Run Code Online (Sandbox Code Playgroud)
Swift有时会给你一些奇怪的编译器错误,这相当于"有多个可能的选项,没有一个工作,所以这里有一个错误".当它抱怨的那个不是你想要的那个时,这可能很奇怪
这就是为什么Swift经常告诉你某些东西"不是Int8"的原因,即使这与你想要做的事情无关(呵呵,我试图连接两个字符串,Int8与任何东西有什么关系? - 这是因为+运营商的一个可能的选择是使用Int8s,这是它选择投诉的那个).
在这种情况下,postNotificationName有多个重载版本,一个有1个参数,一个有2,一个有3(你想要的那个).它们都不适合你提供的类型,所以它说"其中一个选项是一个带有2个参数的调用,你提供了3个,所以这不起作用,还有一个额外的参数".
不幸的是,这真的很令人困惑,并让你摆脱了实际上错误的气味.假设您剪切并粘贴了实际代码,看起来有拼写错误MPMoviePlayerPlaybackDidFinishNotification,并且userInfo参数标签后面缺少冒号.
(ps你不需要在返回void的函数的末尾显式地返回一个返回值,尽管它不会造成任何伤害)