Objective-C declares a class function, initialize(), that is run once for each class, before it is used. It is often used as an entry point for exchanging method implementations (swizzling), among other things. Its use was deprecated in Swift 3.1.
This is what I used to do:
extension NSView {
public override class func initialize() {
// This is called on class init and before `applicationDidFinishLaunching`
}
}
How can I achieve the same thing without initialize
?
I need it for a framework, so requiring calling something in the AppDelegate is a no-go. I need it called before applicationDidFinishLaunching
.
I really like this solution. It's exactly what I'm looking for, but it's for iOS. I need it for macOS. Could someone suggest a macOS version of that?
To be specific, I need the equivalent of this, but for macOS:
extension UIApplication {
private static let runOnce: Void = {
// This is called before `applicationDidFinishLaunching`
}()
override open var next: UIResponder? {
UIApplication.runOnce
return super.next
}
}
I've tried overriding various properties on NSApplication
with no success.
The solution needs to be in pure Swift. No Objective-C.
An initializer is a special type of function that is used to create an object of a class or struct. In Swift, we use the init() method to create an initializer. For example, class Wall { ... // create an initializer init() { // perform initialization ... } }
init() Creates an instance of the app using the body that you define for its content.
EDIT: Since I wrote this answer, the OP has added "pure Swift" to the question in an edit. However, I am leaving this answer here because it remains the only correct way to do this at the time of this writing. Hopefully, module initialization hooks will be added in Swift 6 or 7 or 8, but as of March 2018, pure Swift is the wrong tool for this use case.
Original answer follows:
Unfortunately, Swift doesn't have any direct equivalent to the old initialize()
and load()
methods, so this can't be done in pure Swift AFAIK. However, if you're not averse to mixing a small amount of Objective-C into your project, this isn't hard to do. Simply make a Swift class that's fully exposed to Objective-C:
class MyInitThingy: NSObject {
@objc static func appWillLaunch(_: Notification) {
print("App Will Launch")
}
}
Then add this short Objective-C file to the project:
#import <Cocoa/Cocoa.h>
static void __attribute__ ((constructor)) Initer() {
// Replace "MyFrameworkName" with your framework's module name.
// You can also just #import <MyFrameworkName/MyFrameworkName-Swift.h>
// and then access the class directly, but that requires the class to
// be public, which pollutes the framework's external interface.
// If we just look up the class and selector via the Objective-C runtime,
// we can keep everything internal.
Class class = NSClassFromString(@"MyFrameworkName.MyInitThingy");
SEL selector = NSSelectorFromString(@"appWillLaunch:");
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:class
selector:selector
name:NSApplicationWillFinishLaunchingNotification
object:nil];
}
With these two pieces of code in place, an app that links against your framework should get "App Will Launch" logged to the console sometime before applicationDidFinishLaunching
is called.
Alternatively, if you already have a public ObjC-visible class in your module, you can do this without having to use the runtime functions, via a category:
public class SomeClass: NSObject {
@objc static func appWillLaunch(_: Notification) {
print("App Will Launch")
}
}
and:
#import <Cocoa/Cocoa.h>
#import <MyFrameworkName/MyFrameworkName-Swift.h>
@interface SomeClass (InternalSwiftMethods)
+ (void)appWillLaunch:(NSNotification *)notification;
@end
@implementation SomeClass (FrameworkInitialization)
+ (void)load {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(appWillLaunch:)
name:NSApplicationWillFinishLaunchingNotification
object:nil];
}
@end
Nope, a Swift alternative to initialize()
doesn't exist, mainly because Swift is statically dispatched, so method calls can't be intercepted. And is really no longer needed, as the method was commonly used to initialize static variables from Objective-C files, and in Swift static/global variables are always lazy and can be initialized with the result of an expression (thing not possible in Objective-C).
Even if it would be possible to achieve something similar, I would discourage you to implicitly run stuff without the knowledge of the framework users. I'd recommend to add a configure
method somewhere in the library and ask the users of your library to call it. This way they have control over the initialization point. There are only a few things worser than having a framework that I linked against, but no longer (or not yet) using, to start executing code without my consent.
It's just saner to give framework users control over when they want the framework code to start. If indeed your code must run at the very beginning of the application lifecycle, then ask the framework users to make the call to your framework entry point before any other calls. It's also in their interest for your framework to be properly configured.
E.g.:
MyFramework.configure(with: ...)
// or
MyManager.start()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With