我正在重写 NSResponder 的mouseEntered(theEvent: NSEvent)
方法,并想检索我在 Swift 中创建 NSTrackingArea 时设置的值。我正在使用此构造函数来创建 NSTrackingArea 并将以下对象作为userInfo
.
let trackerData = ["myTrackerKey": view]
let trackingArea = NSTrackingArea(rect: trackingRect, options: [.EnabledDuringMouseDrag, .MouseEnteredAndExited, .ActiveInActiveApp], owner: self, userInfo: trackerData)
(view : NSView
以及trackingRect : NSRect
确实存在)
我正在设置trackerData
,userInfo
以便稍后在mouseEntered(theEvent: NSEvent)
.
override func mouseEntered(theEvent: NSEvent) {
// This does not work
let data1 = theEvent.userData as [NSObject : AnyObject]
let data2 = theEvent.userData as [String : NSView]
let data3 = theEvent.userData as NSDictionary
}
我的代码基于Apple 提供的这个Objective-C 示例代码。引用第 372 行SuggestionsWindowController.m
:
HighlightingView *view = [(NSDictionary*)[event userData] objectForKey: kTrackerKey];
那么我怎样才能阅读事件的userData
?
我看过这个 StackOverflow Q&A但它不起作用。