我正在开发一个包含 Gamecontroller 的应用程序。我正在尝试显示连接的控制器数量,但它不起作用。
这是我的简单代码:
import Cocoa
import GameController
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
// - MARK: Controllers
let controllers = GCController.controllers()
print(controllers.count)
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
它总是打印 0
我究竟做错了什么?
编辑:
问题是我错过了添加:
let ctr = NotificationCenter.default
ctr.removeObserver(self, name: .GCControllerDidConnect, object: nil)
ctr.removeObserver(self, name: .GCControllerDidDisconnect, object: nil)
连接控制器时处理