我有这段代码:
class NotificationsController: NSObject {
static var classNotifier: AnyClass {
if #available(iOS 10, *) {
return UNUserNotificationCenter.classForCoder()
} else {
return UILocalNotification.classForCoder()
}
}
static func foo() {
NotificationsController.classNotifier.foo()
}
}
地点:
protocol Notificable: class {
static func foo()
}
extension UNUserNotificationCenter: Notificable {
static func foo() {
// do something
}
}
extension UILocalNotification: Notificable {
static func foo() {
// do something
}
}
在 Debug模式下构建工作正常。
当我在 Release模式(用于归档)中构建时,编译器说 AnyClass 没有名为“foo”的函数。
现在,我知道如果我删除 Release optimizations build works,但我认为这将是错误的解决方案。
其他解决方案?
最佳答案
一段时间后,我会调查代码并重写您的解决方案。问题与 protocol Notificable
和实例函数 func foo()
有关。
您尝试将此函数作为类函数而不是实例函数来调用,但出现了问题。
工作解决方案示例。
protocol Notificable: class {
static func foo()
}
extension UNUserNotificationCenter: Notificable {
internal static func foo() {
}
}
extension UILocalNotification: Notificable {
internal static func foo() {
}
}
class NotificationsController: NSObject {
static var classNotifier: AnyClass {
if #available(iOS 10, *) {
return UNUserNotificationCenter.classForCoder()
} else {
return UILocalNotification.classForCoder()
}
}
static func foo() {
classNotifier.foo()
}
}
关于ios - Swift 动态类型调试/发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42762688/