如何使用单例/共享类的委托方法?有一个单例类定义了一些协议,但我不知道如何在其他类中访问委托函数。
参考代码片段(Swift):
protocol AClassDelegate
{
func method1()
}
class A
{
static let shared = A()
override init() {
//initialisation of member variables
}
var delegate: AClassDelegate
func foo() {
}
}
class B: AClassDelegate
{
func bar() {
// Note: not getting call to 'foo' method of class 'A'
A.shared.delegate = self
A.shared.foo()
}
}
这个实现是否正确?