Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sprite Kit iOS7 - SKNode UserData property not storing values

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

like image 649
DogCoffee Avatar asked Sep 29 '13 00:09

DogCoffee


1 Answers

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]);
like image 181
LearnCocos2D Avatar answered Sep 22 '22 19:09

LearnCocos2D