Create a dictionary property list programmatically

后端 未结 4 1881
清酒与你
清酒与你 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:14

    I am not sure I understand the basic objective here.

    It seems like at runtime, you are constructing a deep dictionary with many child nodes. But you are constructing this all with static code... why can you not simply make a plist (like the one you had an image of) and read that into an NSDictionary? Both NSDictionary and NSArray have methods that let you simply read in a file and get a whole filled out object. Then it is WAY easier to edit and to understand. That method is dictionaryWithContentsOfFile.

    If all of the data is truly created at runtime before it is put into the dictionary, then it seems like you would want a very different, recursive, style of code rather than the flat examples given.

    Lastly, I personally dislike the dictionaryWithObjects:forKeys: method in NSDictionary for building a dictionary. If you have to do things that way I greatly prefer the alternate method dictionaryWithObjectsAndKeys: which does the same thing but keeps the keys with the objects:

    NSDictionary *item1 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"Screen J",
                           @"Title",
                           [NSNumber numberWithInt:3],
                           @"View"];
    

提交回复
热议问题