Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

ios - Method load() defines Objective-C class method 'load', which is not permitted by 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. }

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.

Link: http://nshipster.com/swift-objc-runtime/

-

More info form dev forums:

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.

Link: https://devforums.apple.com/message/1102025#1102025

-

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...