Reputation: 2030
I have created an NSTrackingArea
and I am passing a Dictionary in the userInfo
argument.
let trackingData = ["section": 1, "sectionRow": 12, "done": false]
let trackingArea = NSTrackingArea(
rect: cellView.bounds,
options: [NSTrackingAreaOptions.MouseEnteredAndExited, NSTrackingAreaOptions.ActiveAlways],
owner: self,
userInfo: trackingData as? [String : AnyObject])
cellView.addTrackingArea(trackingArea)
This event is successfully received here;
override func mouseEntered(event: NSEvent) {
print("Mouse Entered \(event.userData)")
}
How can I read the values for section etc from userData?
Upvotes: 3
Views: 1033
Reputation: 2647
The documentation reads:
When handling such an event you can obtain the dictionary by sending
userData
to theNSEvent
object.
But it seems to be wrong. I never managed to somehow cast or map NSEvent.userData
(which has type UnsafeMutableRawPointer?
) back to [String: Any]
. Instead you can do this:
override func mouseEntered(with event: NSEvent) {
let userInfo = event.trackingArea!.userInfo!
}
Upvotes: 0
Reputation: 285069
Using your syntax
if let userData = event.trackingArea?.userInfo as? [String : AnyObject] {
let section = userData["section"] as! Int
}
But if you pass the done
key as Int
with value 0
or 1
rather than Bool
, you don't need cast the values of the dictionary because it's distinct [String:Int]
let trackingArea = NSTrackingArea(rect: ... , userInfo: trackingData)
and
if let userData = event.trackingArea?.userInfo as? [String : Int] {
let section = event.userData["section"]!
}
The optional bindings are for safety, if there are more events to receive and track.
Upvotes: 4