ios - Swift 动态类型调试/发布

标签 ios swift compiler-optimization

我有这段代码:

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/

相关文章:

ios - 如何在同一函数中再次调用部分代码?

ios - 当按钮收到事件时,UITapGestureRecognizer 到 cellForRowAt

c++ - 优化编译器如何决定何时以及展开多少循环?

ios - Vuforia 视频播放

iphone - 我的应用程序需要时间进行初始启动

iOS 应用程序更新和用户默认值

ios - Swift:在 Safari 中打开 Web View 中的所有外部链接,而不是在 Web View 中

c - 是否允许编译器优化浮点常量乘法

使用指针复制对循环进行 C++ 编译器优化

ios - 如何在 UIViewController 中对 UICollectionView 进行单元测试