方法 load() 定义了 Objective-C 类方法“load",这是 Swift 1.2 不允许的 [英] Method load() defines Objective-C class method 'load', which is not permitted by Swift 1.2

查看:36
本文介绍了方法 load() 定义了 Objective-C 类方法“load",这是 Swift 1.2 不允许的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Parse 并且我正在创建一个符合 PFSubclassing 协议的 PFObject 子类!一切正常,但现在我使用的是 Swift 1.2,它给了我这个错误:

I'm using Parse and I'm creating a PFObject subclass conforming to the protocol PFSubclassing! It was working all fine, but now I'm using Swift 1.2 and it gives me this error:

1. override class func load() {
2.      self.registerSubclass()
3. }

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

On line 1: Method 'load()' defines Objective-C class method 'load', which is not permitted by Swift 1.2

有人遇到过这个问题吗?我该如何解决?

Anyone have this problem yet? How can I fix?

推荐答案

NSHispster 有一篇关于 method swizzling 的文章,在不同的上下文中涉及到这个:

There is an NSHispster article about method swizzling that touches on this in different context:

不幸的是,在 Swift 中实现的加载类方法从来没有由运行时调用,使该推荐变得不可能.相反,我们只能在第二个选项中进行选择:

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:

  • 在 initialize 中实现方法 swizzling. 这可以安全地完成,只要您在执行时检查类型并将 swizzling 包装在dispatch_once(无论如何你都应该这样做).

  • 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).

在应用程序委托中实现方法 swizzling. 无需通过类扩展添加方法 swizzling,只需向应用程序添加方法委托时执行application(_:didFinishLaunchingWithOptions:) 被调用.根据您正在修改的类,这可能就足够了,应该保证你的代码每次都被执行.

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/

-

来自开发论坛的更多信息:

More info form dev forums:

Swift 1.1 允许您使用class func"定义+load"方法load()",但它们实际上并未在您的应用程序启动时运行就像 Objective-C +load 方法一样.Swift 1.2 禁止它们以避免这可能会奏效.

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 中似乎是处理此类事情的好地方.

tl;dr initialize() and didFinishLaunchingWithOptions seem to be decent places for such things in Swift.

这篇关于方法 load() 定义了 Objective-C 类方法“load",这是 Swift 1.2 不允许的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆