我正在覆盖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,但它不起作用。
答案 0 :(得分:1)
如果您想从活动中阅读trackerData
,您必须从跟踪区域获取用户信息
if let userInfo = theEvent.trackingArea?.userInfo {
let trackerData = userInfo["myTrackerKey"]! as! NSView
// do something with the view
}