4

I am using the userData of an SKSpriteNode to save custom data into it. I have initialized the userData with value through this line of code:

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

I then 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 crash with error Fatal error: unexpectedly found nil while unwrapping an Optional value

Printing the value in the first chunk of code returns

init value = Optional(0.01)

while printing the value from the second chunk returns this

using value = nil

I am not able to figure out why my saved userData just disappears.

Thanks in advance

  • Where do you use first chunk of code, and where the second chunk comes from? Personally, I would say, that you are operating on different node which doesn't have `userData` initialized at all. – Whirlwind Apr 18 '17 at 21:47

0 Answers0