我创建了一个简单的WatchApp节拍器。 我使用的NSTimer与.scheduledTimerWithTimeInterval,我有呼叫错误额外的参数“选择”
谢谢您的回答
func playBeat() {
if(self.State == true) {
self.State == false
[labelPlayPause.setTitle("Pause")]
} else {
self.State == true
[labelPlayPause.setTitle("Play")]
}
BPMValue = 10
var BPMInt:Int = Int(BPMValue)
let value = "\(BPMInt) BPM"
labelBPM.setText(value)
let aSelector: Selector = "playBeat"
dispatch_async(dispatch_get_main_queue(), {
NSTimer.scheduledTimerWithTimeInterval(60/self.BPMValue, target:self, selector: aSelector, userInfo:nil, repeats:false)
})
}
这是斯威夫特一个贫穷的错误信息!
这到底是什么意思是,你需要确保类型每个函数参数的匹配类型传递的值的。
在你的情况, BPMValue
是Float
,并scheduledTimerWithTimeInterval
期待和NSTimeInterval
作为第一个参数。 需要注意的是NSTimeInterval
( Double
)和Float
是不等价的。 在Objective-C,你得到一个隐式转换,这不会在雨燕发生。
尝试
NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(60/self.BPMValue), target:self, selector: aSelector, userInfo:nil, repeats:false)
作为一个侧面说明,你可以稍微更简洁与斯威夫特您的代码:
func playBeat() {
if State { // If State is a Bool, you can lose the '== true'
State = false // Must use set not comparison operator. No need to refer to 'self'.
labelPlayPause.setTitle("Pause")
} else {
State = true // Must use set not comparison operator.
labelPlayPause.setTitle("Play")
}
BPMValue = 10
var BPMInt = Int(BPMValue) // Int Type is inferred
let value = "\(BPMInt) BPM"
labelBPM.setText(value)
let aSelector: Selector = "playBeat"
dispatch_async(dispatch_get_main_queue(), {
NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(60/self.BPMValue), target:self, selector: aSelector, userInfo:nil, repeats:false)
})
}