Really need help on data structures

Discussion in 'iOS Programming' started by MACloop, Aug 2, 2010.

  1. macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #1
    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
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    If you are trying to build a database then use a database! Seriously. CoreData...
     
  3. thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #3
    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
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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...
     
  5. thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #5
    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
     

Share This Page