我有这样的事情:
class SocketManager {
static let instance: SocketManager
var socket = Socket()
}
extension SocketManager: SocketDelegate {
func someFunc() {
}
}
Socket
是一个拥有protocol
和delegate
protocol SocketDelegate: class {
func someFunc()
}
class Socket {
weak var delegate: SocketDelegate?
}
如何将socket.delegate
设为SocketManager.instance
?
答案 0 :(得分:1)
您设置另一个类以符合SocketDelegate
协议并将委托设置为该类。执行此操作后,SocketDelegate
可以调用委托方法,这些方法将传递给您将委托设置为的类。
你的课程将是这样的:
class SocketManager {
static let instance: SocketManager
var socket = Socket()
weak var delegate: SocketDelegate?
}
extension SocketManager: SocketDelegate {
func someFunc() {
}
}
protocol SocketDelegate: class {
func someFunc()
}
class Socket: SocketDelegate { // so it conforms to the delegate
SocketManager.instance.delegate = self
}