SpriteKit SKNode user data returning null

I am using userData for SKSpriteNode to save user data. I initialized userData with a value through this line of code:

oscIcon.userData = NSMutableDictionary()
oscIcon.userData?.setValue("0.01",forKey: "rValue")
print("init value = \(oscIcon.userData!["rValue"])")

Then I use this userData value in another function:

print("using value = \(oscIcon.userData!["rValue"])")
let tempValHold = oscIcon.userData!["rValue"] as! CGFloat
selectedNode.zRotation = tempValHold + (translation.y * 0.01)

However, I am getting a failure with an error. Fatal error: nil unexpectedly found while deploying optional value

A listing of the value in the first code fragment returns

init value = Optional(0.01)

when printing values ​​from the second fragment returns this

using value = nil

I cannot understand why my saved UserData just disappears.

Thanks in advance

+4
source share

Source: https://habr.com/ru/post/1674937/


All Articles