Create a dictionary property list programmatically

Discussion in 'iOS Programming' started by ulquiorra, Jul 14, 2009.

  1. ulquiorra macrumors member

    Joined:
    Jun 17, 2009
    #1
    Hello all ,

    I seem to be a little stuck on the creating propertly list issue.
    I want to create a dictionary which feeds data to my uitableview but I seem to be getting some hard luck.
    I want to create a dictionary that resembles this one ( give or take a couple of items less )

    ItemFour.jpg (image)

    I've looked at this tutorial
    Property List Programming Guide: Creating Property Lists Programmatically

    and I came up with a small sample of my own

    Code:
    //keys
    
    NSArray *Childs = [NSArray arrayWithObjects:@"testerbet", nil];
    
    NSArray *Children = [NSArray arrayWithObjects:@"Children", nil];
    
    NSArray *Keys = [NSArray arrayWithObjects:@"Rows", nil];
    
    NSArray *Title = [NSArray arrayWithObjects:@"Title", nil];
    
    //strings
    
    NSString *Titles;
    
    Titles = @"mmm training";
    
    //dictionary 
    
    NSDictionary *item1 = [NSDictionary dictionaryWithObject:Childs, Titles forKey:Children , Title];
    
    NSDictionary *item2 = [NSDictionary dictionaryWithObject:Childs, Titles forKey:Children , Title];
    
    NSDictionary *item3 = [NSDictionary dictionaryWithObject:Childs, Titles forKey:Children , Title];
    
    NSArray *Rows = [NSArray arrayWithObjects: item1, item2, item3, nil];
    
    NSDictionary *Root = [NSDictionary dictionaryWithObject:Rows forKey:Keys];
    
    
    // NSDictionary *tempDict = [[NSDictionary alloc] //initWithContentsOfFile:DataPath];
    
    NSDictionary *tempDict = [[NSDictionary alloc] initWithDictionary: Root];
    
    it's obvious that i'm trying to use this data of hierachy for my tableviews..
    I'm actually using this example

    Drill down table view with a detail view - iPhone SDK Articles

    So I was wondering how can I can create my propertylist( dictionary ) programmatically so that I can fill it with my own arrays.

    those of you who read it thnx in advance
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    If you have static data to be displayed in a table view then using a plist is a convenient way to create the static data. I do this in a lot of my apps.

    Building the plist file is simple. Just build up your data structure with dictionaries, arrays, strings, and numbers and then write out the container object with writeToFile:atomically: and that's it. Don't bother with the stuff in that Apple doc about plists. writeToFile:atomically: does all the hard stuff related to archiving of the plist.

    Then add this plist file to your project and make sure its copied to the app bundle. Then in your code when you need the data just use initWithContentsOfFile: and you've gotten back your data. Remember that all the contents will be immutable.

    So in your example write out Root. Then add the plist file to your project. And then read it back in whenever you need it.
     
  3. ulquiorra thread starter macrumors member

    Joined:
    Jun 17, 2009
    #3
    First of all thnx for your respons.
    Let me explain what I want , it could be that my approach is wrong, so if it is let me know ;)

    I have an xmlparser that parses a xmlfile and saves information in different arrays.
    Code:
    <menu_mob>
    <menuitem id="1" text="exercise" pic="workout"/>
    <menuitem id="2" text="VirtuaGym online" pic="online"/>
    <menuitem id="3" text="testing nib" pic="settings"/>
    </menu_mob>
    
    <workout_mob name="Morning Workout" location="home" equipment="none" level="Beginner" time="7" id="MHXB7" pic="1">
    <exerciseref id="CW003"/>
    kleine test
    		
    <exerciseref id="XF002"/>
    <exerciseref id="XL002"/>
    <exerciseref id="XB001"/>
    <exerciseref id="XC004"/>
    <exerciseref id="XA002"/>
    <exerciseref id="XA003"/>
    </workout_mob>
    −
    <workout_mob name="Morning Workout" location="home" equipment="none" level="Beginner" time="15" id="MHXB15" pic="2">
    <exerciseref id="CM001"/>
    <exerciseref id="XA003"/>
    <exerciseref id="GB002"/>
    <exerciseref id="GC001"/>
    <exerciseref id="GL001"/>
    <exerciseref id="DI001"/>
    <exerciseref id="DS001"/>
    <exerciseref id="GT001"/>
    <exerciseref id="CW010"/>
    </workout_mob>
    
    In this case my menu attribute text is saved in an array called firstScreen[]
    the second array called secondScreen[] has the information from attribute name workout tag. My parser does this( and a little more).
    This is where it gets tricky for me since I'm new to iPhone development.
    I want firstScreen to be displayed on the first tableview and secondscreen to be displayed after row exercise is pressed. I want to have a similar construction when I press for instance the first Morning workout ( that one has different exercises ).

    So here is where I figured that i needed a tree structure which was dynamic but I wanted to test it first with some static code(which I would later try to make dynamic).
    I guess something like this
    Code:
    NSMutableArray *Rows = [NSMutableArray arrayWithCapacity: 1];
    
    for (int i = 0; i < 4; ++i) {
      NSMutableArray *theChildren = [NSMutableArray arrayWithCapacity: 1];
      [theChildren addObject: [NSString stringWithFormat: @"tester %d", i]];
    NSString *aTitle = [NSString stringWithFormat: @"Item %d", i];
      NSDictionary *anItem = [NSDictionary dictionaryWithObjectsAndKeys: aTitle, @"Title", theChildren, @"Children"];
      [Rows addObject: anItem];
    }
    
    NSDictionary *Root = [NSDictionary withObject: Rows andKey: @"Rows"];
    
    So when I delete <exerciseref id="DS001"/> in my xml then my row would be deleted as well.

    So what you said is indeed true
    However my app is depends on my xmlinput.
    Any suggestions?

    update:
    I've thought it over and you are absolutely right! This is indeed also an option .... I responded a little too fast.
    I build it wrote it to a file and now I have to copy to my bundle ..
    Do you know how I can copy it to my bundle ?
    again thnx for the advice this is indeed a lot easier then the stuff in the Apple Docs
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    That part I do manually. Just copy/move the plist file to your app folder somewhere. (I have a Resources folder.) Drag it into the project window into the Resources group and it should just be copied to the app bundle when you build the app.
     
  5. ulquiorra thread starter macrumors member

    Joined:
    Jun 17, 2009
    #5
    aiiii that was actually something I also wanted to do code wise :(
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    I don't understand the goal. Are you trying to automate your build or are you trying to build a plist while the app is running and then later read it back in?
     
  7. ulquiorra thread starter macrumors member

    Joined:
    Jun 17, 2009
    #7
    sorry again spoke too soon .. I got what I wanted;)

    thnx !!
     

Share This Page