Nesting NSMutableDictionaries inside NSMutableArrays inside NSMutableDictionaries - objective-c

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!

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..

Related

NSArray of NSStrings Subtraction/Difference

I have 2 NSArrays with a bunch of NSStrings. I want to do an array subtraction, so I get all the elements from Array1 that are only in Array1 and not also in Array2. These NSString objects are different objects, but with the same string values.
Is there an easy way to do this, or do I need a double loop? In python, for example I could use a set operation, but I'm not sure how to do it in Obj-C.
NSMutableSet gives you much of set operations you get in python

NSDictionary with same object key

i have a NSDictionary with arrays
A1: a,b,c,d,e,f and
A2:1,1,1,2,2,3
i want to get the objects of key 1
i used:
[dicitonay objectsForKey: #"1"];
but i am get only 1 object, how to get all objects?
If your dictionary contains two arrays, when you do 'objectForKey', one of the arrays(a single array object) will be returned. Without looking at your actual code, you should now have a single array object. You can use the typical array methods to access the objects inside the array.

Filling NSMutableArray for later use in obj-c

How do you fill a NSMutableArray with a set capacity for later use?
Basically I want to set up a NSMutableArray to act as a map for my game objects, so I have this line...
gameObjects = [[NSMutableArray alloc] initWithCapacity:mapWidth*mapHeight];
Which I had hoped would create and fill my MutableArray so I can get then access it with this kind of index...
int ii = (cellY*mapWidth)+cellX;
NSDictionary *currentObject = [gameObjects objectAtIndex:ii];
But I just learned initWithCapacity doesn't fill the array, so should I create blank objects to fill it with, or is there a Null that I can fill it with? Also would I do that with 2 for loops or is there an instruction something like "initWith:myObject" ?
I want to be able to check at a certain index within the array to see if there's an object there or not, so I need to be able to acces that index point, and I can only do that if there's something there or I get an out of bounds error.
I'll be using this NSMutableArray pretty much as a grid of objects, it's a 1 dimensional array organised as a 2 dimensional array, so I need to be able to fill it with mapWidth*mapHeight of something, and then calculate the index and do a check on that index within the array.
I've looked on here and googled but couldn't find anything like what I'm asking.
Thanks for any advice.
I think what you are looking for is [NSNull null]. It is exactly what you want- a placeholder value.
You can find more information on the topic in this question.
initWithCapacity is just a performance optimization -- it has no effect on the array behavior, it just keeps the code "under the covers" from having to repeatedly enlarge the internal array as you add more entries.
So if you want a "pre-allocated" array, you'd need to fill it with NSNull objects or some such. You can then use isKindOfClass to tell if the object is the right type, or simply == compare the entry to [NSNull null]. (Since there's only ever one NSNull object it always has the same address).
(Or you could use a C-style array of pointers with nil values for empty slots.)
But you might be better off using an NSMutableDictionary instead -- no need to pre-fill, and if the element isn't there you get a nil pointer back. For keys use a NSNumber object that corresponds to what would have been your array index.
initWithCapacity only hints to NSMutableArray that it should support this many objects. It won't actually have any objects in it until you add them. Besides, every entry in the array is a pointer to an object, not a struct like you'd normally have in a standard c array.
You need to change how you're thinking about the problem. If you don't add an object to the array, it's not in there. So either you pre-fill the array with "empty" objects as you've said, which is weird. Or you can add the objects as you need them.

Creating an array from properties of objects in another array

Is there any convenient way to take an array/set of objects and create a new array/set containing some property of each item in the first array?
For example, an array contains Car objects. I need an array of licensePlates, where each car has an NSObject car.licensePlate.
Currently I just iterate through the first array adding objects to my mutable results array, but was wondering if there is an instantiation method that exists for this (checked the docs for NSArray).
This will return an array containing the value of licensePlate from each item in the myCars array:
NSArray *licensePlates = [myCars valueForKeyPath:#"licensePlate"]
If you want only unique items (for example), you can do something like this:
NSArray *licensePlates = [myCars valueForKeyPath:#"#distinctUnionOfObjects.licensePlate"];
For more possibilities, see the Collection Operators documentation in the Key-Value Coding Programming Guide.

taking out objects from NSMutableArray

I have an array which contains objects some may be same and some are different.
How can I take each same objects and different objects separately ?
Below is the array
NSMutableArray *items = [[NSMutableArray alloc]
initWithArray:[NSArray arrayWithObjects:#"rat", #"rat", #"cat",#"Lion", #"cat", #"dog", #"dog", nil]];
I want to have four arrays which will contains these items :
First array with two rats
2nd array with two cats
3rd array with one lion
4th array with two dogs
What could be the best way to take the objects out ? Identical object should be placed in same array.
Here's a general answer:
Put the array into an NSCountedSet - that will store each object and a count of the number of times it has been added.
Then - for each object in this counted set create an array with that object repeated according to the count of each object.
It will work in your case, because you are using static strings, which are going to be the same if they are the same string. This will take more work if you are using custom objects.
But the real question we have to ask is why you need to create these repetitive structures. If we could know what you are doing with it, we could give you better advice about how to go about it. For example, if you just need to keep a running count of the number of each type of object you have, you could just use the NSCountedSet directly (it descends from NSMutableSet, so it is already mutable) and not bother with creating the arrays.