swift - MASShortcut 没有这样的模块

标签 swift xcode macos cocoa cocoapods

我一直在尝试使用 MASShortcut并按照那里的说明使用 cocoapods 添加它。然后我将它添加到我的 -Bridging-Header.h 文件并在我的主 swift 文件中导入它,但我不断收到错误

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/

相关文章:

iphone - 如何在iOS中自定义搜索键盘?

swift - 将 Pod 更新到 Firebase 5.4 后出现 Firebase 错误

macos - Mac 重新启动后出现意外的 -psn_0_65552 命令行参数

swift - 空RLMArray的 Realm 查询

cocoa - 为什么 gcov 不报告我的单元测试覆盖的任何行?

ios - 无法设置 translatesAutoresizingMaskIntoConstraints

swift - 如何在 swift macOS 中旋转 PDF 页面?

objective-c - 将 PDFPage 添加到 PDFDocument 时 PDFView 不更新

ios - swift 火力地堡 : Update specific objects resulting from Firebase query

swift - => 运算符在 Swift 中意味着什么?