我一直在尝试使用 MASShortcut并按照那里的说明使用 cocoapods 添加它。然后我将它添加到我的 导入
它,但我不断收到错误
No such module 'MASShortcut'
这是我的设置:
AppDelegate.swift:
import Cocoa
import Carbon
import MASShortcut
var kShortCut: MASShortcut!
@IBOutlet weak var shortcutView: MASShortcutView!
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
override func awakeFromNib() {
... omitted ...
}
override func viewDidLoad() {
super.viewDidLoad()
shortcutView.shortcutValueChange = { (sender) in
let callback: (() -> Void)!
if self.shortcutView.shortcutValue.keyCodeStringForKeyEquivalent == "k" {
self.kShortCut = self.shortcutView.shortcutValue
callback = {
print("K shortcut handler")
}
} else {
callback = {
print("Default handler")
}
}
MASShortcutMonitor.sharedMonitor().registerShortcut(self.shortcutView.shortcutValue, withAction: callback)
和我的Podfile:
target 'myapp' do
# Comment this line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
pod 'MASShortcut', '~> 2'
# Pods for myapp
target 'myappTests' do
inherit! :search_paths
# Pods for testing
end
end
最后proj-Bridging-Header.h:
#import <Cocoa/Cocoa.h>
#import <MASShortcut/Shortcut.h>
最佳答案
AppDelegate 应该是这样的。请注意缺少任何 import MASShorcut
。
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var kShortCut: MASShortcut!
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var shortcutView: MASShortcutView!
override func awakeFromNib() {
shortcutView.shortcutValueChange = { (sender) in
let callback: (() -> Void)!
if self.shortcutView.shortcutValue.keyCodeStringForKeyEquivalent == "k" {
self.kShortCut = self.shortcutView.shortcutValue
callback = {
print("K shortcut handler")
}
} else {
callback = {
print("Default handler")
}
}
MASShortcutMonitor.shared().register(self.shortcutView.shortcutValue, withAction: callback)
}
}
}
桥接头应如下所示:
#ifndef Testin_Bridging_Header_h
#define Testin_Bridging_Header_h
#import <MASShortcut/Shortcut.h>
#endif /* Testin_Bridging_Header_h */
Shortcut.h
导入所有其他头文件。 Testin 是我的应用程序的名称(当然是为了测试)
一些其他提示:
- 确保在应用程序的build设置中设置桥接 header 。
- 如果最初没有构建框架,请尝试从 MASShortcut 方案构建。
- AppDelegate 没有生命周期事件,您需要使用 NSViewController 或 NSWindowController 子类来使用生命周期方法(即 viewDidLoad)。
关于swift - MASShortcut 没有这样的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38650572/