Create a dictionary property list programmatically

后端 未结 4 1877
清酒与你
清酒与你 2021-02-06 00:40

I want to programatically create a dictionary which feeds data to my UITableView but I\'m having a hard time with it. I want to create a dictionary that resembles this property

4条回答
  •  无人及你
    2021-02-06 01:00

    first of .. super! thanks .. I really appreciate the explanation and code snippet. Since you gave me such a good explanation I hope you don't mind me asking a couple more questions.

    First, I did as you suggested and this is what I came up with : (I used my original property list instead of the example this time so this is where the drilldown table gets his( or needs to get his ) treestructure).

    http://img509.imageshack.us/img509/7523/picture2lsg.png

    NSDictionary *root = [NSMutableDictionary dictionary];
    NSDictionary *item1 = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:@"VirtuaGym Online"] forKey:[NSArray arrayWithObjects:@"Title"]];
    NSDictionary *item2 = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:@"Do the training"] forKey:[NSArray arrayWithObjects:@"Title"]];
    NSDictionary *item3 = ... 
    
    [root setObject:item1 forKey:@"Item 1"];
    [root setObject:item2 forKey:@"Item 2"];
    

    Also did some research and tried something else with some other input..

    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"];
    

    I decided to just test both of these however it does what I want. It gives me a EXC_BAD_ACCESS error.

    I was also wondering since I saw you using number in your code snippet, couldn't you also use NSString since that's what the plist uses.. could be totally of here of course

    NSDictionary *item1 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Screen J",[NSNumber numberWithInt:3],nil]
                                                      forKeys:[NSArray arrayWithObjects:@"Title",@"View",nil]];
    

    and third a question is about my possible approach to my app. I have an xml parser which saves certain information in different arrays. I want to use this information for my drilldown UITableviews (infoFirstScreen[] infoSecondScreen[] infoThirdScreen[]). The information provided has to be connected like the tree I showed you above. This is the reason I wanted to build the dictionary in code so I can take the info from my arrays and insert it here. My question do you think my approach is correct, wrong or is there a faster way?

    again really appreciate the explanation ;)

提交回复
热议问题