Last active
May 4, 2019 08:01
-
-
Save iby/ca7a73d193bb2ce54a5954cd70acda1f to your computer and use it in GitHub Desktop.
Using NSObject’s +load and +initialize from Swift, checkout the full story – https://medium.com/post-mortem/using-nsobjects-load-and-initialize-from-swift-f6f2c6d9aad0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <MyProject/MyProject-Swift.h> | |
// This was a more concise solution in Swift 4.2 and earlier. If you try this with Swift 5 you'd get "Swift class | |
// extensions and categories on Swift classes are not allowed to have +load methods" runtime error. | |
// @implementation Foo (private) | |
// + (void)load { [self swiftyLoad]; } | |
// + (void)initialize { [self swiftyInitialize]; } | |
// @end | |
// This is what you'd want to use with Swift 5, basically just a pure Objective-C defined class. | |
@interface Loader : NSObject | |
@end | |
@implementation Loader : NSObject | |
+ (void)load { [Foo swiftyLoad]; } | |
+ (void)initialize { [Foo swiftyInitialize]; } | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
public class Foo: NSObject { | |
@objc public static func swiftyLoad() { | |
Swift.print("Rock 'n' roll!") | |
} | |
@objc public static func swiftyInitialize() { | |
Swift.print("Hello initialize!") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment