2017-03-13 13 views
3

をリリース私は、コードのこの部分ました:スウィフト動的な型デバッグ/

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 
    } 

} 

ビルデバッグモードでは正常に動作します。
リリースモード(アーカイブ用)でビルドすると、AnyClassには "foo"という関数がないことがコンパイラによって示されます。

リリース最適化のビルド作業を削除しても、私は間違った解決策になると思います。

その他のソリューション?

+0

'classNotifier'は' Class'を返しますが、 'foo'はインスタンスメソッドです。また、おそらくあなたのプロトコルをNotifiableと呼びます。 – Edgar

+0

'(NotObjectとしてNotificationsController.classNotifier).foo()'を試したことがありますか? –

+0

プロトコルでfooを作成しようとしましたか?静的なfunc fooのような静的には気付きますか? – Suen

答えて

0

しばらくしてから、私はコードの調査を行い、解決策を書き直しました。問題は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() 
    } 

} 
+0

ポイントのおかげです。実際に私の間違いですが、実際のコードで私は既に静的メソッドを持っています(私は質問を更新します)。まだ問題が残っています –

+0

@ LucaDavanzo NotificationsControllerとExtensionsは別のファイルにありますか? –

+0

実際にはい... –

関連する問題