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

查看:883
本文介绍了方法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文章在不同的背景下涉及到这一点:

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:


  • 实施方法调整初始化。这可以安全地完成,只要你在执行时检查类型并将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).

在app委托中实现方法调整。只需在app
委托中添加一个方法,而不是通过类扩展添加方法。
应用程序(_: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 /

-

更多信息表单dev论坛:

More info form dev forums:


Swift 1.1允许您使用class func
load()定义+ load方法,但它们实际上并不是在你的应用程序启动时运行
就像Objective-C +加载方法一样。 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 似乎是斯威夫特这类事情的好地方。

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天全站免登陆