ios - 方法 load() 定义了 Objective-C 类方法 'load' ,这是 Swift 1.2 不允许的

标签 ios objective-c swift parse-platform

我正在使用 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/

相关文章:

ios - 如何在 iOS 的 UIDatePicker 中显示从现在起仅 7 天?

Swift DispatchQueue 与 Firebase 同步

android - Ionic/AngularJS 菜单栏刷新按钮到当前 View Controller

iphone - 如何将对象移回我的 View

c++ - Apple C 模块进入 Xcode 工具链是否有时间表?

iOS Instagram 标签文本布局?它是如何完成的?

任何协议(protocol)上的 Swift 2.0 协议(protocol)扩展

ios - 如何在 swift 中实现 uiviewController 的自定义初始化方法

iphone - 如何在 UItableview 中添加带加载的页脚 View ?

ios - Swift ios 无法让动画无限