Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MACloop

macrumors 6502
Original poster
May 18, 2009
393
0
Germany
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:

Code:
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
 
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
 
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...
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.