2016-07-29 118 views
1

我一直嘗試使用MASShortcut並按照說明我已經使用cocoapods添加它。然後我把它添加到我的< PROJ> -Bridging-Header.h文件和import編在我的主迅速文件,但我不斷收到錯誤MASShortcut沒有這樣的模塊

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 

最後凸出橋接-Header.h

#import <Cocoa/Cocoa.h> 
#import <MASShortcut/Shortcut.h> 

回答

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進口的所有其他頭文件。試驗在爲我的應用程序的名稱(當然要測試)

一些其他提示:

  • 確保您在設置應用程序的構建設置的橋接報頭。
  • 如果最初沒有構建框架,請嘗試從MASShortcut方案構建。
  • AppDelegate沒有生命週期事件,您需要使用NSViewController或NSWindowController子類來使用生命週期方法(即viewDidLoad)。
+0

謝謝你的幫助。我傳遞了'no such module'錯誤,但現在正在''shortcutView.shortcutValueChange = {(sender)in'行中發生'致命錯誤:意外地發現nil,同時展開可選值'。 – Sparkmasterflex

+0

@Sparkmasterflex可能意味着你沒有連接你的插座,因此它是零。 –

相關問題