How to maintain the order of key added in NSMutableDictionary - ios

I didn't knew this at first place, I thought that the key/value pair I am adding to my dictionary will be in the same order when I try to retrieve it back...
Now here is my question -
I have a ViewController which I have made as singleton, inside this VC I have defined:
#property (nonatomic) NSMutableDictionary *dictionary;
Now I try accessing this dictionary from various other classes and set its contents via:
[[[ViewController sharedViewController] dictionary] setValue:[NSNumber numberWithBOOL:NO] forKey:#"xx"] , yy ,zz and so on
I have my delegates implemented which would hit for a particular key(xx,yy,zz and so on). Now whenever a particular delegate method is hit I need to update my dictionary with same key but [NSNumber numberWithBOOL:YES] which is happening .. but the order in which I added the items in dictionary is not maintained ... How can I achieve this?? I need to maintain the order in the same way in which I have added my items to the dictionary at first place??
Can someone please help me out??

As pointed, you should use NSArray to have your values ordered. You can also use some 3rd party APIs like: M13OrderedDictionary or others, however, that's not a pretty solution.
Obviously, the desired solution is to create your own object or struct and keep an array of those objects/structs.

NSDictionary is just a mapping between keys and values that has no defined order. If you try to iterate over the keys, you may or may not get them back in the same order you added them, and you should never depend on the ordering of the keys when iterating over them.
One thing you could do is create an NSArray that contains the keys in the order you want them. Then you can iterate over the keys in the array and get the values from the NSDictionary in the order you wanted. The combination of these two basically gives you a sorted key NSDictionary.
Another thing you could do is to just use an NSArray to stores the values, unless the keys that you're using matter as well. But if all you're trying to do is get the values in a certain order, then I would say NSArray should work fine.

You can make a copy of key by using the following code:
NSArray *allKeys = [dictionary allKeys];
Then, the keys all saved in order and you can access particular value by getting specific key in allKeys array and retrieving it by using dictionary[allKeys[2]].

Related

Changes in NSMutableDictionary not reflecting in parent NSDictionary

Question 1)
I am creating a NSMutableDictionary from a NSDictionary using the following code:
_allKeysDictionary = [receivedDictionary mutableCopy];
and I am updating the values from the _allKeysDictionary using the following code:
[_allKeysDictionary setObject:[textField text] forKey:#"field"];
But my parent NSDictionary that is receivedDictionary is not reflecting those changes made in _allKeysDictionary.
I need the values of receivedDictionary to be updated.
Question 2)
I am using
(__bridge CFMutableDictionaryRef)
to keep a pointer to one of the NSDictionary in my JSON response. but when I am trying to regain the above CFMutableDictionaryRef, I am still getting NSDictionary. I don't know what is wrong. I am using the following code to regain the Dictionary from the reference
NSMutableDictionary *getPointedDict = (__bridge NSMutableDictionary*) dataRefValue;
Q1: But the method mutableCopy even says so: A copy is created! The receivedDictionary logically won't be changed.
To update (after you're done):
receivedDictionary = _allKeysDictionary;
or to be safe that no changes can be made later:
receivedDictionary = [NSDictionary dictionaryWithDictionary: _allKeysDictionary];
By the way, you can write:
_allKeysDictionary[#"field"] = textField.text;
Q2: In Obj-C (and many other languages) casting does not change the structure of the object. If it's an NSDictionary, it will always stay an NSDictionary. So the actual cast will work (i.e. the compiler won't tell you), but once you start doing things with the object that are not implemented in the original class, your app will crash or you'll get other undefined results.
But my parent NSDictionary that is receivedDictionary is not reflecting those changes made in _allKeysDictionary
Of course they are not, you made a mutableCopy - that is a copy. You've now got two dictionaries with the same keys and values.
I need the values of receivedDictionary to be updated
If it is an NSDictionary you cannot do this, that is what immutable means. You can replace the reference stored in receivedDictionary to one which references your new mutable dictionary (or an immutable copy of it), but that doesn't do what you wish you have other variables storing references to the original dictionary.
However if the values in the dictionary are themselves mutable then you can alter those values. E.g. if you NSDictionary contains NSMutableArray values then you can change the elements of those arrays, but you cannot change which array is associated with which key in the dictionary.
Q2: You cannot simply cast a reference to an immutable dictionary to make it into a mutable one. Casting doesn't change the referenced object in anyway, it just changes the type the compiler treats it as (and if the type is not compatible with the actual type your program breaks).
It looks like you are unsure about objects, references and mutability; probably time to do some studying.
HTH
MutableCopy & copy both do deep copy on NSDictionary. The only difference is that mutableCopy makes your new dict mutable.
When you do deep copy, you are copying the value, not the reference, which mean whatever thing you do on your new dictionary, it won't affect the old value.
Plus, your original dict is immutable, even if you copy the reference, its value won't change.

How to sort UITableView headers according to declaration order

So I have a little problem here, I think I made my point clear in the title of this post. I want to sort UITableView headers according to the order I declared them in code. I tried surfing the net already but I can't find any solution.
I think one from here can help. Thanks in advance!
EDIT: This is my code to of the table view contents
NSDictionary *temp = [[NSDictionary alloc]initWithObjectsAndKeys:accSettingsOpts, #"Account",notifSettingsOpts,#"Notifications",aboutOpts,#"About",logoutOpt,#"Sign Out",nil];
I want to display the table view sorted by
-Account
-Notifications
-About
-Sign Out
But it displays as
-About
-Account
-Notifications
-Sign Out
EDIT: This is how the problem is addressed.
Based from the answer provided in which I accepted below, I modified it as
#interface myClass ()
NSArray *headerArr;
#end
at viewDidload I added
headerArr = [[NSArray alloc]initWithObjects:#"Account",#"Notifications",#"About",#"Sign Out", nil];
and lastly...
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [headerArr objectAtIndex:section];
}
Thanks for the help guys. I didn't imagine that it's just that simple.
A dictionary is an unordered collection. If you store your items in a dictionary, the order in which you defined them is not saved anywhere.
Table views are an inherently ordered list of objects. Therefore a dictionary is not suitable as a way of storing the items for a table view. An array, however, makes a natural data source for a table view.
Use an array rather than a dictionary. It's as simple as that. You can use an array of dictionaries, or an array of some other data objects, and then write your cellForRowAtIndexPath method to extract the data from your model (array elements) and install the values in the cell.
The reason the order is different from the declaration order is that NSDictionary is unordered: it is sorted based on the hash code of your strings, which is rather arbitrary.
To force a particular order, use a container with a fixed order, such as NSArray. For example, you can store your accSettingsOpts, notifSettingsOpts, and so on, in a single array, add a header property to the Opts class, and pull that property to set section headers in your UITableView.

Create a copy of a NSManagedObject

I need to temporarily store the content of a NSManagedObject into a dictionary. Because core data has its own memory management procedures, I don't want to keep any strong pointers to the NSManagedObject's fields, only the values are of interest at this point (values are passed between view controllers, the MOCs are different). I can't create weak pointers either because I want to control when the memory reclaim is done.
I tried a few things, all failed or did not fit the purpose.
a duplicate [[myNSMO alloc] initWithEntity:[NSEntityDescription entityForName:entity inManagedObjectContext:myNSMO.managedObjectContext] insertIntoManagedObjectContext:nil];
It's working, but does not fit into my app design (without getting into details)
generate a NSDictionary from the NSManagedObject, using [myNSMO dictionaryWithValuesForKeys:<#(NSArray *)#>]. That's not ok because it returns a dictionary with the addresses of the NSManagedObject fields.
create a NSDictionary populating each key-value using a copyWithZone, like this
[myDictionary setObject:[myNSMO.field copyWithZone:nil] forKey:#"Key"];
Doesn't work either, I still get the field address...
Manually enter each field with
[myDictionary setObject:[NSString stringWithFormat:#"%#",myNSMO.field ] forKey:#"Key"];
It's fine this time, I do get new memory allocation. But that's highly time consuming to code this manually...
Any chance that someone found clever way to do that? the reason option 1) did not work is because I use the dictionary as a queue. I first store a copy of the object, then pop the entry out when required. A copy of that particular dictionary entry is then returned to the asking method. The problem is that I can't create a copy of an NSManagedObject that was created using [[...] insertIntoManagedObjectContext:nil];
Any solutions?
It's safe to keep strong pointers to the fields of a managed object in most senses — relationships are special but the actual Foundation objects of dates, strings and numbers are ordinary objects that'll stay in memory if you have a strong reference.
That being said, to create a dictionary copy containing all the properties of an entity you could do something like:
NSArray *properties = [[object entity] properties];
NSMutableDictionary *dictionaryRepresentation = [NSMutableDictionary dictionary];
for(NSAttributeDescription *attribute in properties)
{
// we want only actual attributes, not relationships
// or fetched properties
if([attribute isKindOfClass:[NSAttributeDescription class]])
{
[dictionaryRepresentation
setObject:[object valueForKey:attribute.name]
forKey:attribute.name];
}
}
So you're using the fact that managed objects expose a description of their entities which includes a list of properties, whittling those properties down to just the attributes, then using key-value coding to fetch the current value of each property and finally inserting it into the dictionary.
If for some reason you did want copies of the properties — though, as I say, there's absolutely no reason to do so — you'd copy (and autorelease if you're not using ARC) each property when inserting it into the dictionary.

NSDictionary with NSObjects as keys

I'm trying to create an NSDictionary that keeps track of calling objects for a function. I'd like to create a unique string for each object without knowing anything about it. My first thought is to use the memory address of the object's pointer, but I'm not sure how to do that.
Any thoughts? I need to use some sort of unique id from an NSObject as the keys in my dictionary.
If your application supports iOS6 only check the NSDictionaryOfVariableBindings macro.
The code would be something like :
// Create the dictionary
NSObject *firstObject = [NSString stringWithString:#"My first item"];
NSObject *secondObject = #"[#"an", #"array", #"of", #"strings"]";
NSDictionary *theDic = NSDictionaryOfVariableBindings(firstObject, secondObject);
// Access data
NSString *singleString = [theDic objectForKey:#"firstObject"];
NSArray *listOfStrings = [theDic objectForKey:#"secondObject"];
My suggestion is not to use a dictionary. If you were to place them into an array, you could think of it as a dictionary with automatically generated unique keys (the indexes). It's really exactly what you are describing. If for some reason you have to use a dictionary, my suggestion is to implement that same model I'm speaking of, but you would have to generate and maintain the keys.
While I agree that your solution sounds like it may not be the best approach, have you considered -hash in the NSObject protocol? All NSObjects should return one. Be forewarned that it's a hash, so there's a chance that two different objects could have the same hash.
You could also consider a category on NSObject that your collection implements. The category could generate a UUID to use as a key.

Add objects to dictionary created by JSONKit?

In my project I have to load a number of json files. I parse them with JSONKit and after every single parsing with
NSMutableDictionary *json = [myJSON objectFromJSONString];
I add them to an array like:
[self.themeArray addObject:json];
This works fine so far. Now I need to pass the dictionaries arround between views. Works so far as well, but I need to add few more objects to the dictionary object-> json. Even it I declared json as NSMutableDictionary it does not allow me to add objects as it seems the JSONKit parser creates non-mutable dictionaries.
I was thinking about creating an object which contains the json dictionary and my additional data side by side so I wouldn´t have to change the json dictionary. I could even change it to NSDictionary because there is no need to change it. But that seems somehow not-elegant to me.
Do you have any idea how I can solve this issue without changing the JSONKit lib?
Thanks in advance!
EDIT
i just tried after changing my code to
NSMutableDictionary *json = [[myJSON objectFromJSONString] mutableCopy];
something like this
[[self.theme objectForKey:#"theme"] setObject:sender forKey:#"sender"];
[[self.theme objectForKey:#"theme"] setValue:sender forKey:#"sender"];
Xcode throws an exception:
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '* -[JKDictionary setObject:forKey:]: mutating method sent to immutable object'
I assume that´s due to the fact there are still nested dictionaries in the superior dictionary. Then i would have to interate through my json object to copy all dictionaries to mutable dictionaries, right?
Perhaps it's better to switch to NSJSONSerialization as suggested by Guillaume.
EDIT
I just tried something like this
[self.theme setValue:sender forKey:#"sender"];
And it works now! It was as i assumend. Only the json object was copied to a mutable object. Probably obvious to you, it was not to me.
Thank you all for your help!
EDIT
Finally i changed my code again after i could not manage to change all objects deep inside my dictionary data to mutable objects. I threw out JSONKit and use now NSJSONDeserialization as recommendet here with the option NSJSONReadingMutableContainers. My code looks now like this and all containers (arrays and dictionaries) are mutable deep inside too. That makes me happy! ;-)
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:myJSON options:NSJSONReadingMutableContainers error:&jsonParsingError];
You can always create mutable versions of objects from their non-mutable counterparts by copying them.
NSMutableDictionary* json = [[myJSON objectFromJSONString] mutableCopy];
It is not optimal, but copying smaller dictionaries does is usually not noticable from a performance point of view.
Even it I declared json as NSMutableDictionary it does not allow me to add objects as it seems the JSONKit parser creates non-mutable dictionaries.
What type the variable is declared at means nothing. You could have declared json as NSNumber and that wouldn't make it an NSNumber.
You need to make a mutable copy of the dictionary (with mutableCopy) to get an NSMutableDictionary.
I have three ideas for your.
Create real data model objects and store them in your array. Use the JSON dictionary to init your object.
Store NSMutableDictionary objects in your array. Pass the JSON dictionary to +[NSMutableDictionary dictionaryWithDictionary:] to init the NSMutableDictionary. Others have suggested calling -[NSDictionary mutableCopy] on the JSON dictionary to do the same thing.
Create a category based on NSDictionary that stores the additional data.
NOTES:
Generally creating classes to represent your data is considered the best option, but it is also the most amount of up front work. Basically you are trading more up front work against more maintenance work as you try to keep up maintaining the dictionaries.
Storing mutable dictionary is exactly what you seem to be asking for, but it may be lots of works to find all the places where JSON dictionaries are added to the array and replacing them with the new call.
Creating a category for NSDictionary means you shouldn't need to change any of your current code, but it requires maintainers to understand how you have enhanced NSDictionary. In addition, it will help separate your changes from the original parsed JSON. You can use associated objects to store the data.

Resources