ViewController.Type 没有名为的成员 [英] ViewController.Type does not have a member named
问题描述
只是一个简单的任务,但我遇到了麻烦.尝试另辟蹊径,但失败了.
Just a simple task, but I'm in trouble. Trying to make a different way but it fails.
如何使用先前声明的变量初始化 NSTimer?var 和 let 都没有帮助.
How to init NSTimer with declared previously variable? Neither var nor let helps.
推荐答案
属性的初始值(在您的情况下:timer
)不能依赖于该类的另一个属性(在您的情况下:间隔
).
The initial value of a property (in your case: timer
) cannot depend on another property of the class (in your case: interval
).
因此你必须将赋值 timer = NSTimer(interval, ...)
移动到一个 method类,例如进入 viewDidLoad
.因此,timer
必须定义为可选或隐式解包可选.
Therefore you have to move the assigment timer = NSTimer(interval, ...)
into a method of the
class, e.g. into viewDidLoad
. As a consequence, timer
has to be defined as an
optional or implicitly unwrapped optional.
还要注意 Selector(...)
将文字 string 作为参数,而不是方法本身.
Note also that Selector(...)
takes a literal string as argument, not the method itself.
所以这应该有效:
class ViewController: UIViewController {
var interval : NSTimeInterval = 1.0
var timer : NSTimer!
func timerRedraw() {
}
override func viewDidLoad() {
super.viewDidLoad()
timer = NSTimer(timeInterval: interval, target: self, selector: Selector("timerRedraw"), userInfo: nil, repeats: true)
// ...
}
// Other methods ...
}
这篇关于ViewController.Type 没有名为的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!