UnsafeMutablePointer<Void> 到 [NSObject : AnyObject]

UnsafeMutablePointer<Void> to [NSObject : AnyObject]

我正在重写 NSResponder 的 mouseEntered(theEvent: NSEvent) 方法并想检索我在 Swift 中创建 NSTrackingArea 时设置的值。我正在使用 this 构造函数创建 NSTrackingArea 并将以下对象作为 userInfo.

传递
let trackerData = ["myTrackerKey": view]
let trackingArea = NSTrackingArea(rect: trackingRect, options: [.EnabledDuringMouseDrag, .MouseEnteredAndExited, .ActiveInActiveApp], owner: self, userInfo: trackerData)

(确实存在 view : NSViewtrackingRect : 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 Sample Code。引用 SuggestionsWindowController.m 的第 372 行:

HighlightingView *view = [(NSDictionary*)[event userData] objectForKey: kTrackerKey];

那么我怎样才能读取事件的userData

我查看了 但它不起作用。

如果要从事件中读取trackerData,则必须从跟踪区域中获取用户信息

if let userInfo = theEvent.trackingArea?.userInfo {
  let trackerData = userInfo["myTrackerKey"]! as! NSView
  // do something with the view
}