我正在使用 Parse,我正在创建一个符合 PFSubclassing 协议(protocol)的 PFObject 子类! 一切正常,但现在我使用的是 Swift 1.2,它给了我这个错误:
1. override class func load() {
2. self.registerSubclass()
3. }
第 1 行:方法 'load()' 定义了 Objective-C 类方法 'load',这是 Swift 1.2 不允许的
有人遇到过这个问题吗?我该如何解决?
最佳答案
有一篇关于方法调配的 NSHispster 文章在不同的上下文中涉及到这一点:
Unfortunately, a load class method implemented in Swift is never called by the runtime, rendering that recommendation an impossibility. Instead, we're left to pick among second-choice options:
Implement method swizzling in initialize. This can be done safely, so long as you check the type at execution time and wrap the swizzling in dispatch_once (which you should be doing anyway).
Implement method swizzling in the app delegate. Instead of adding method swizzling via a class extension, simply add a method to the app delegate to be executed when application(_:didFinishLaunchingWithOptions:) is called. Depending on the classes you're modifying, this may be sufficient and should guarantee your code is executed every time.
链接:http://nshipster.com/swift-objc-runtime/
-
更多信息来自开发论坛:
Swift 1.1 allowed you to define "+load" methods with "class func load()", but they were not actually run at startup time of your app like Objective-C +load methods are. Swift 1.2 bans them to avoid the impression that this might work.
链接:https://devforums.apple.com/message/1102025#1102025
-
tl;dr initialize()
和 didFinishLaunchingWithOptions
似乎是 Swift 中此类事情的合适位置。
关于ios - 方法 load() 定义了 Objective-C 类方法 'load' ,这是 Swift 1.2 不允许的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28422468/