UnsafeMutablePointer <void>到[NSObject:AnyObject]

时间:2015-07-19 18:24:29

标签: swift cocoa nsevent nsresponder

我正在覆盖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 : 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 Sample Code。引用SuggestionsWindowController.m的第372行:

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

那么如何阅读活动的userData

我查看了this StackOverflow Q&A,但它不起作用。

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您想从活动中阅读trackerData,您必须从跟踪区域获取用户信息

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