我有这样的功能:
func register<T: Routable>(_ action: Action, withCallback callback: @escaping (T?) -> Void) {
notificationCenter.addObserver(forName: actionMap[action], object: nil, queue: .main, using: { notification in
let routable = T(userInfo: notification.userInfo)
callback(routable)
})
}
其中Routable定义如下:
protocol Routable {
init?(userInfo: [AnyHashable: Any]?)
}
当我尝试使用它时,我收到
无法显式特化泛型函数
这是用法:
controller.register<Navigate>(Action.navigate) { [unowned self] navigate in
// do something
}
有什么想法可以让编译器开心吗?