userData
A dictionary containing arbitrary data.
Declaration
SWIFT
var userData: NSMutableDictionary!
OBJECTIVE-C
However, in Objective-C the userData member is initially nil. Given that this is the same class then it should also be in Swift and using it, e.g.
let node = SKSpriteNode(imageNamed: name)
node.userData["action"] = Action() // custom class
causes a crash:
fatal error: Can't unwrap Optional.None
This is because the it is in fact nil despite the '!' following the Swift definition. This must be a documentation bug. The correct code is:
let node = SKSpriteNode(imageNamed: name)
node.userData = NSMutableDictionary()
node.userData["action"] = Action() // custom class
No comments:
Post a Comment