从 Swift 调用 Objective-C -> 在范围内找不到类型 'AppDelegate'
Calling Objective-C from Swift -> Cannot find type 'AppDelegate' in scope
我已阅读 this question,我相信我的问题有所不同。
我有一个 swift class SwiftUtils.swift
已经在我的项目中调用 objective c 代码并且我有一些 objective c 代码调用回到 swift.
所以我得到了桥接 header 东西,一切正常。
我的问题是我正在 swift 中构建系统菜单并尝试在我的 AppDelegate 中调用一个方法,如下所示:
@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)
}
我无法让编译器在函数的第一行解析 AppDelegate
。
Xcode 一直告诉我 “在范围内找不到类型 'AppDelegate'”
我的桥接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 */
我的AppDelegate.h:
#import <Cocoa/Cocoa.h>
#import "Foo-Swift.h"
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property SwiftUtils *utils;
- (void) checkForUpdates;
+ (void) showSystemStatus;
@end
据我所知,showSystemStatus 方法是一种 class 方法(例如静态),您正试图从实例中调用此方法。也许这可能是您问题的根源。
以下示例应该适合您:
let status = NSMenuItem(title: "System Status",
action: #selector(AppDelegate.showSystemStatus),
keyEquivalent: "s")
为了防止循环,您可以将您的实现移至另一个 class 并试一试。
我已阅读 this question,我相信我的问题有所不同。
我有一个 swift class SwiftUtils.swift
已经在我的项目中调用 objective c 代码并且我有一些 objective c 代码调用回到 swift.
所以我得到了桥接 header 东西,一切正常。
我的问题是我正在 swift 中构建系统菜单并尝试在我的 AppDelegate 中调用一个方法,如下所示:
@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)
}
我无法让编译器在函数的第一行解析 AppDelegate
。
Xcode 一直告诉我 “在范围内找不到类型 'AppDelegate'”
我的桥接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 */
我的AppDelegate.h:
#import <Cocoa/Cocoa.h>
#import "Foo-Swift.h"
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property SwiftUtils *utils;
- (void) checkForUpdates;
+ (void) showSystemStatus;
@end
据我所知,showSystemStatus 方法是一种 class 方法(例如静态),您正试图从实例中调用此方法。也许这可能是您问题的根源。
以下示例应该适合您:
let status = NSMenuItem(title: "System Status",
action: #selector(AppDelegate.showSystemStatus),
keyEquivalent: "s")
为了防止循环,您可以将您的实现移至另一个 class 并试一试。