gpt4 book ai didi

swift : Extra Argument in call with scheduledTimerWithTimeInterval

转载 作者:搜寻专家 更新时间:2023-11-01 05:59:12 28 4
gpt4 key购买 nike

我创建了一个简单的 WatchApp 节拍器。我将 NSTimer 与 .scheduledTimerWithTimeInterval 一起使用,但出现错误 Extra Argument 'selector' in call

感谢您的回答

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)
})

}

最佳答案

这是一条来自 Swift 的错误消息!

这真正意味着您需要确保每个函数参数的类型与传递的值的类型相匹配。

在您的例子中,BPMValue 是一个FloatscheduledTimerWithTimeInterval 是期望值,NSTimeInterval 作为它的第一个参数.请注意,NSTimeInterval (Double) 和 Float 并不等效。在 Objective-C 中你会得到一个隐式转换,这在 Swift 中不会发生。

尝试

NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(60/self.BPMValue), target:self, selector: aSelector, userInfo:nil, repeats:false)

作为旁注,您可以使用 Swift 编写更简洁的代码:

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)
})

}

关于 swift : Extra Argument in call with scheduledTimerWithTimeInterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27875005/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com