Works with Objective-c:
AppDelegate.h
@interface AppDelegate: NSObject{
IBOutlet NSMenu *statusMenu;
NSStatusItem * statusItem;
}
@end
AppDelegate.m
@implementation AppDelegate
- (void )applicationDidFinishLaunching:(NSNotification *)aNotification
{
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[statusItem setMenu:statusMenu];
[statusItem setTitle:@" Status Menu"];
[statusItem setHighlightMode:YES];
}
@end
But if I try to basically do the same in swift It does nothing.
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet var statusMenu: NSMenu;
< br /> func applicationDidFinishLaunching(aNotification: NSNotification?) {
let bar = NSStatusBar.systemSt atusBar()
let statusItem = bar.statusItemWithLength(CGFloat(NSVariableStatusItemLength))
statusItem.title = "Status Menu"
statusItem.menu = statusMenu
statusItem. highlightMode = true
}
)
There is no error, it just didn’t do anything. The function applicationDidFinishLaunching is called because its internal println() creates output.
p>
Does anyone know what I am doing wrong here?
This should make your Swift code work:
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet var statusMenu: NSMenu;
var statusItem: NSStatusItem?;
func applicationDidFinishLaunching( aNotification: NSNotification?) {
let bar = NSStatusBar.systemStatusBar()
statusItem = bar.statusItemWithLength(CGFloat(NSVariableStatusItemLength))
statusItem!.title = "Status Menu"
statusItem!.menu = statusMenu
statusItem!.highlightMode = true
}
}
Me Try to use swift to add a simple status menu to the status bar, but it won’t be displayed.
Works with Objective-c:
AppDelegate.h
< p>
@interface AppDelegate: NSObject{
IBOutlet NSMenu *statusMenu;
NSStatus Item * statusItem;
}
@end
AppDelegate.m
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[statusItem setMenu:statusMenu];
[statusItem setTitle:@"Status Menu"];
[statusItem setHighlightMode:YES];
}
@end
But if I try Basically do the same thing in swift, it does nothing.
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet var statusMenu: NSMenu;
func applicationDidFinishLaunching(aNotification: NSNotification?) {
let bar = NSStatusBar.systemStatusBar()
let statusItem = bar.statusItemWithLength(CGFloat(NSVariableStatusItemLength) )
statusItem.title = "Status Menu"
statusItem.menu = statusMenu
statusItem.highlightMode = true
}
}
There is no error, it just didn’t do anything. The function applicationDidFinishLaunching is called because the println() inside it creates the output.
Does anyone know what I’m doing? What is wrong here?
The problem here is that after applicationDidFinishLaunching is executed, the statusItem will be out of scope, and the execution will release the object. This is not the case in Objective-C code Situation, because the statusItem variable is declared at the class level.
This should make your Swift code work:
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet var statusMenu: NSMenu;
var statusItem: NSStatusItem?;
func applicationDidFinishLaunching(aNotification: NSNotification?) {
let bar = NSStatusBar.systemStatusBar()
statusItem = bar.statusItemWithLength(CGFloat(NSVariableStatusItemLength))
statusItem!.title = "Status Menu"
statusItem!.menu = statusMenu
statusItem!.highlightMode = true
}
}