Calling Objective-C from Swift -> Cannot find type 'AppDelegate' in scope

Viewed 1708

I have read this question and I believe my problem is different.

I have a swift class SwiftUtils.swift that is already making calls to objective c code in my project and I have some objective c code making calls back to swift.

So I've got the bridging header stuff all working as it should.

My issue is I am building a system menu in swift and am trying to call a method in my AppDelegate like this:

@objc func createMenuItem() {
    let appDelegate = NSApplication.shared.delegate as? AppDelegate
    let status = NSMenuItem(title: "System Status",
                            action: #selector(appDelegate.showSystemStatus(_:)),
                            keyEquivalent: "s")
    status.target=appDelegate.self
    statusItem.menu?.insertItem(status, at: 0)
}

I can't get the compiler to resolve AppDelegate on the first line of the func.

Xcode keeps telling me "Cannot find type 'AppDelegate' in scope"

enter image description here

My bridging header:

#ifndef Foo_Bridging_Header_h
#define Foo_Bridging_Header_h

#import <Foundation/Foundation.h>
#import "uiframework/Utils.h"
#import "AppDelegate.h"

#endif /* Foo_Bridging_Header_h */

My AppDelegate.h:

#import <Cocoa/Cocoa.h>
#import "Foo-Swift.h"

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property SwiftUtils *utils;

- (void) checkForUpdates;
+ (void) showSystemStatus;

@end
1 Answers

As far as I can understand, showSystemStatus method is a class method (e.g. static) and you are trying to call this method from instance. Maybe it can be the root of your issue.

Following example should work for you:

let status = NSMenuItem(title: "System Status",
                action: #selector(AppDelegate.showSystemStatus),
                keyEquivalent: "s")

For preventing the cycling, you can move your implementation to another class and give it a try.

Related