我在和斯威夫特胡闹。我有一个协议被定义为
protocol timerProtocol {
func timerFired()
}持有对委托的引用的类
class Stopwatch: NSObject {
var delegate: protocol <timerProtocol>
init(delegate: protocol <timerProtocol> ) {
self.delegate = delegate
}
...
}和一个实现该协议的类
class StopwatchesTableViewController: UITableViewController, timerProtocol {
func timerFired() {
println("timer");
}
let stopwatch = Stopwatch(delegate: self) // Error here
...
}声明秒表时出现错误--“类型'StopwatchesTableViewController -> () -> StopwatchesTableViewController!‘不符合协议'timerProtocol'”
如何解决此问题?
发布于 2014-06-06 08:27:12
更改var delegate: protocol <timerProtocol>
转到var delegate: timerProtocol?
发布于 2014-06-22 20:03:19
从语法和逻辑上讲,这对我来说就像一个护身符:
protocol TimerProtocol {
func timerFired()
}
class Stopwatch {
var delegate: protocol <TimerProtocol>? = nil
init() { }
convenience init(delegate: protocol <TimerProtocol> ) {
self.init()
self.delegate = delegate
}
}
class StopwatchesTableViewController: UITableViewController, TimerProtocol {
@lazy var stopwatch: Stopwatch = Stopwatch()
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
stopwatch.delegate = self
}
func timerFired() {
println("timer");
}
}注意:协议的名称应以大写字母开头。
或
StopwatchesTableViewController类将如下所示:
class StopwatchesTableViewController: UITableViewController, TimerProtocol {
var stopwatch: Stopwatch? = nil
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
stopwatch = Stopwatch(delegate: self)
}
func timerFired() {
println("timer");
}
}发布于 2014-06-06 08:13:43
试着改变,
let stopwatch = Stopwatch(delegate: self) // Error here至
@lazy var stopwatch: Stopwatch = Stopwatch(delegate: self)https://stackoverflow.com/questions/24071730
复制相似问题