PDA

View Full Version : Really need help on data structures




MACloop
Aug 2, 2010, 02:39 PM
Hi,
I would like to organize data in categories but can't figure out how to do this. I want it to be like in a database when you have a 1:N relation

In my app I have an array with objects and another array with the categories.

Lets take a very simple example:

If I have the class Fruit like this:
NSString *name;
NSString *country;
NSString *sort;

The objects could be:
fruit1 -> name:aName1 country:india sort:apple
fruit2 -> name:aName2 country:sweden sort:pear
fruit3 -> name:aName3 country:UK sort:banana
fruit4 -> name:aName4 country:China sort:apple
fruit5 -> name:aName5 country:USA sort:pear
fruit6 -> name:aName6 country:Mexico sort:banana

all fruits are saved into the array fruits

the categories are:
apple
banana
pear


So, I want to examine something like this:

for(Fruit *f in fruits) {
if f.sort is one of the categories...
here I want to organize the fruits somehow... as sublevels in the categories array?
}

Any ideas on how to organize the data?
Thanks in advance!
MACloop



robbieduncan
Aug 2, 2010, 02:52 PM
If you are trying to build a database then use a database! Seriously. CoreData...

MACloop
Aug 3, 2010, 02:32 AM
If you are trying to build a database then use a database! Seriously. CoreData...

I know - the problem is that the app is already designed and implemeted and this is an additional feature which was not supposed to be included at first. It is only neccesary in one small part of the program and I do not want to rewrite the whole data management in the app because of this. Therefore I thought if I could solve it in another way it would be good. I have looked into core data abit...is it possible to use for a small part of data within an app?

Thanks for your answer!
MACloop

robbieduncan
Aug 3, 2010, 03:21 AM
If you only want one indexed column (the category) just use a NSMutableDictionary of NSArrays.

In all seriousness working out the best data structure for your app is a core programming skill and one that should be taught on any good computer science course...

MACloop
Aug 3, 2010, 04:36 AM
If you only want one indexed column (the category) just use a NSMutableDictionary of NSArrays.

In all seriousness working out the best data structure for your app is a core programming skill and one that should be taught on any good computer science course...

hmm...yes I will certanly consider core data next time I plan an app! This time I have to go with the dict and array solution, even if it is not optimal...

MACloop