NSDictionary with same object key - objective-c

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.

Related

Parse query where key contains any object in array

My parse class has a property called "postcodes", which is an array of numbers.
In my app, I have an array of "relevantPostcodes". I want to create a query where the postcodes key contains any object from the "relevantPostcodes" array.
Something like [query whereKey:#"postcodes" containsAnyObjectInArray:relevantPostcodes"]. Any ideas?
You can use [query whereKey:#"postcodes" containedIn:relevantPostcodes]. See the description in the doc. Just like the equal to methods, I think it will operate on array type attributes and do what you need.

How to enumerate through dictionary in Objective-C in the order in which they were defined

Is it possible to enumerate NSDictionary in the order in which the key-value is defined?
There's no built-in way to do this. You'll have to store the order as a property of the added objects or use NSNumber as the keys.
Nope. From the documentation:
allKeys Returns a new array containing the dictionary’s keys.
(NSArray *)allKeys Return Value A new array containing the dictionary’s keys, or an empty array if the dictionary has no entries.
Discussion The order of the elements in the array is not defined.
The only way you can do this is by creating an array of keys first.
Then use that array, iterating over it to create the dictionary.
Dictionaries do not have any inherent concept of order.

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

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.