Sprite Kit iOS7 - SKNode UserData property not storing values
Asked Answered
P

1

7

I would think this work work easily, however I cannot understand why my NSMutableDictionary property is not working as I would have expected.

[self.testSprite.userData setValue:@"CAT" forKey:@"key"];
NSLog(@"%@", [self.testSprite.userData objectForKey:@"key"]);
NSLog(@"%lu", [self.testSprite.userData count]);

I am retuning (null) and 0.

Is there a special trick for using the spriteNode userdata ?

Thanks

Psycholinguistics answered 29/9, 2013 at 0:32 Comment(0)
C
20

The userData property is initially nil. You have to create a dictionary and assign it first:

self.testSprite.userData = [NSMutableDictionary dictionary];

[self.testSprite.userData setValue:@"CAT" forKey:@"key"];
NSLog(@"%@", [self.testSprite.userData objectForKey:@"key"]);
NSLog(@"%lu", [self.testSprite.userData count]);
Conquian answered 29/9, 2013 at 8:40 Comment(1)
This almost killed me...thanks! Why trying to setValue on a nil doesn't throw a warning/error is beyond me.Cryoscopy

© 2022 - 2024 — McMap. All rights reserved.