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.
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()
andload()
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:Then add this short Objective-C file to the project:
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:
and:
As pointed out previously by others it is neither possible (nor good programming) to do what you ask for in a Framework in Swift. Achieving the functionality from the application itself (where this sort of behaviour belongs) is fairly simple though - no need to mess with notifications or selectors. You simply override the
init
of yourNSApplicationDelegate
(orUIApplicationDelegate
) and set up your class initializer there:And the corresponding static function:
This will achieve the same functionality as
initialize()
.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.: