Matt Facer
Matt Facer

Reputation: 3105

unique identifier for array of items in iphone

I'm currently planning (and part coding) a new app. I have an array of items which the user creates. This is effectively the library. It's easiest to think of this as the classic ingredients and meal.

I have a library of ingredients. I then have a library of meals. Each meal will contain several ingredients.

So the relationship is one meal to many ingredients. Each ingredient can be used in multiple meals. I am OK setting all this up - but I am having a think about how the ingredients will be identified within the meal. If I delete an ingredient, I then want to delete the reference in each meal where it may appear.

Should I be assigning a random ID (maybe a 5 digit number) for each ingredient the user creates, then when they add this to a meal, I use the 5 digit ID as the identifier. When a user deletes an ingredient, I then loop through each meal array and remove any references to the said ingredient 5 digit code.

OR... am I completely over thinking this?! Thanks for any help!!

Upvotes: 1

Views: 157

Answers (2)

Joseph Tura
Joseph Tura

Reputation: 6360

How about just using sqlite for storing both ingredients and meals? Don't know what you are actually coding, but it sounds like a good application for a database.

If you don't need to permanently store the data (e.g. you are actually talking about a game or something), you could still use a db with temporary tables).

Upvotes: 1

Brad
Brad

Reputation: 11515

Just do an: [item hash]

But...

It sounds like you want to just create an array of references from your "meals" to your "ingredients". An NSArray or NSMutableArray will retain your "ingredient" - and will release it when the array is released.

Upvotes: 0

Related Questions