Jimmery
Jimmery

Reputation: 10139

Nesting NSMutableDictionaries inside NSMutableArrays inside NSMutableDictionaries

So I have some XML with a structure like this:

<wdata>
    <wid>abc123</wid>
    <wname>Main Name</wname>
    <wauthor>Author</wauthor>
    <plist>
        <pdata>
            <pid>def456_0</pid>
            <pname>Sub Name</pname>
            <ilist>
                <idata>
                    <iid>ghi789_0</iid>
                    <iname>iName</iname>
                    <ivalue>32</ivalue>
                </idata>
                <idata>
                    <iid>ghi789_1</iid>
                    <iname>iName</iname>
                    <ivalue>23</ivalue>
                </idata>
            </ilist>
        </pdata>
        <pdata>
            <pid>def456_1</pid>
            <pname>Sub Name</pname>
            <ilist>
                <idata>
                    <iid>ghi789_2</iid>
                    <iname>iName</iname>
                    <ivalue>24</ivalue>
                </idata>
                <idata>
                    <iid>ghi789_3</iid>
                    <iname>iName</iname>
                    <ivalue>42</ivalue>
                </idata>
            </ilist>
        </pdata>
    </plist>
</wdata>

So the <plist> can contain any number of <pdata>, and the <ilist> in each <pdata> can contain any number of <idata>.

I want to store this data in Objective C. Im guessing the best approach is for <wdata> to be an NSMutableDictionary object, with <plist> being an NSMutableArray containing an array of NSMutableDictionary objects for each <pdata>, and then each <ilist> are NSMutableArrays containing arrays of NSMutableDictionary objects for each <idata>. (!?)

My problem is Im struggling to access arrays within dictionaries, and im struggling to add dictionaries to arrays within dictionaries.

For instance I am using the following code to add a NSMutableDictionary to <plist>:

[[wdata objectForKey:@"plist"] addObject:pDictCopy];

But I get "unrecognised selector" errors on compiling.

Can anyone help me access and add data to my nested dictionaries and arrays?

Thanks in advance!

Upvotes: 0

Views: 90

Answers (1)

vishy
vishy

Reputation: 3231

Here to perform this line

[[wdata objectForKey:@"plist"] addObject:pDictCopy];

Your [wdata objectForKey:@"plist"] must return an NSMutableArray, this you can cross verify by NSLog, and check does it contains an NSMutableArray?.

And the best approach is first create all the objects, means setting the objects for dictionaries, adding the dictionaries to array, and then set these arrays to dictionaries and later on..

Upvotes: 2

Related Questions