【问题标题】:MASShortcut no such moduleMASShortcut 没有这样的模块
【发布时间】:2016-12-03 16:25:25
【问题描述】:

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

没有这样的模块'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> 

【问题讨论】:

    标签: swift xcode macos cocoa cocoapods


    【解决方案1】:

    这就是 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 是我的应用程序的名称(当然是为了测试)

    其他一些提示:

    • 确保在应用程序的构建设置中设置桥接头。
    • 如果最初没有构建框架,请尝试从 MASShortcut 方案构建。
    • AppDelegate 没有生命周期事件,您需要使用 NSViewController 或 NSWindowController 子类来使用生命周期方法(即 viewDidLoad)。

    【讨论】:

    • 感谢您的帮助。我通过了no such module 错误,但现在在shortcutView.shortcutValueChange = { (sender) in 行上得到fatal error: unexpectedly found nil while unwrapping an Optional value
    • @Sparkmasterflex 可能意味着您没有连接插座,因此它是 nil。
    猜你喜欢
    • 2018-08-10
    • 2017-02-09
    • 2019-02-21
    • 2018-03-21
    • 2021-12-07
    • 2023-03-27
    • 2018-12-18
    • 2018-11-16
    • 2016-12-19
    相关资源
    最近更新 更多