Search code examples
objective-cpropertiesextending

Objective-C - Adding some properties without extending class, is there a way?


Here is the problem :( While I am working with Spritekit nodes I need some settings to be accessible inside SKSpriteNode, SKLabelNode classes etc... Settings are stored in pretty basic types (string, array, floats).SpriteNode have property "name" but that is not enough for me, I need some array field instead because I have few more settings needed...

Is there a way , somehow, to add those settings to let's say SKNode class so they can be a part of SKSPriteNode and SKLabelNode classes?

I dont want to make NSDictionary field as a member of MyScene (SKScene) class to store all settings in format key->value,because accessing them requires more unnecessary programming logic.

I just want to, create node with settings inside, read those settings directly from that node, and based on settings make an further action.


Solution

  • If I'm reading your needs correctly, then it sounds like you just want to use the userdata property. Every SKNode has an NSDictionary property called userdata. You just have to initialize it and then add your data.