iDevGames Forums
NSMutableArray help - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: NSMutableArray help (/thread-9962.html)

NSMutableArray help - xctive - Apr 3, 2012 02:38 PM

Hello everyone I a bit new to iOS programming so here goes.

I have a class called sceneObjects and from that I have a few subclasses "Ants" and "Food". All of these are stored in a NSMutableArray of sceneObjects. The problem is that Ants need to know the location of Food so how do I access the right elements and give them other elements information within the same array.

Thank you for your time.

RE: NSMutableArray help - skyhawk - Apr 3, 2012 04:59 PM

create 2 more arrays called antObjects and foodObjects and stick ants and foods into each respectively.

The cost storage of doing so is negligible, and now you can easily iterate through them.

The slow way would be to iterate over each sceneObject and test the type (or have the class store some "type" you can query on)

RE: NSMutableArray help - SethWillits - Apr 3, 2012 09:49 PM

*ahem* SceneObject, not sceneObject, please. Stick to Obj-C conventions when using Obj-C. Smile

Other than that, ditto to skyhawk.

RE: NSMutableArray help - xctive - Apr 4, 2012 11:48 AM

Thanks skyhawk that sounds like just what I need to do. Grin