Reputation: 193
I've been trying to play with Swift 3, but I am unable to get started. The following code is compiling, however it does not log anything. Looks like applicationDidFinishLaunching
is not being called. Am I missing some critical piece here?
Sources/main.swift:
import AppKit
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification) {
NSLog("application start")
}
}
NSApplication.shared()
NSApp.setActivationPolicy(.regular)
let controller = AppDelegate()
NSApp.delegate = controller
NSApp.run()
p.s. There is a similar question about applicationDidFinishLaunching
being called but not printing anything. I believe that this is not the case, as having window.orderFrontRegardless()
instead of logging also has no effect for me.
System Version: OS X 10.11.6
> swift --version
Apple Swift version 3.0 (swiftlang-800.0.43.6 clang-800.0.38)
Target: x86_64-apple-macosx10.9
Upvotes: 5
Views: 5444
Reputation: 575
There could be deletion of Application scene from storyboard. This happens with me as well. So in this case, you can create a new empty project and follow steps below -
You can change title by clicking on it and can change top menu as well.
Upvotes: 0
Reputation: 321
Should work with MacOS 11+
Since none of the answers had the component I am using, here is my approach. Keep in mind, that I do all of it in the same file but that is not necessary.
First import the necessary kit:
import Appkit // For Macos
import UIKit // For Mobile
Add the delegate
Then add the AppDelegate class under your main App struct. When you write the class (without the function) and start typing func app... Xcode will already give you possible suggestions. When you find the right function just autocomplete it and let Xcode generate it for you.
class AppDelegate: NSObject, NSApplicationDelegate { // For MacOS
func applicationDidFinishLaunching(_ notification: Notification) {
print("did start")
NSLog("application start")
}
}
For Mobile use UIApplicationDelegate instead of NSApplicationDelegate
In your main struct add your delegate like this in a property wrapper.
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate // MacOS
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate // Mobile
Upvotes: 0
Reputation: 145
for me... I had no windows visible at launch. As soon as I ticked that box on one of the windows, applicationDidFinishLaunching was called when I ran the app.
Upvotes: 0
Reputation: 2464
I'd a similar problem where func applicationDidFinishLaunching(_ aNotification: Notification)
was not getting called.
For me, the issue was with the App Delegate referencing outlet.
Solution: Just removing it and then connecting it back had resolved the issue for me.
This is how you can recreate a connection
Upvotes: 3
Reputation: 361
The line where you refer to, but don't use NSApplication.shared
looks like a typo. If you replace it with NSApp = NSApplication.shared
, and make sure the method signature in AppDelegate is correct it, should work.
Upvotes: 0
Reputation: 47876
If you want to implement Objective-C protocols in Swift 3, you need to use _
indicating the method has no label for the first parameter.
func applicationDidFinishLaunching(_ aNotification: Notification) {
(UPDATE)
Sorry, in my first code, I have forgotten to to replace NSNotification
with Notification
. (Thanks, Leo Dabus.)
Upvotes: 4