I am trying to set a key for UserName as with respect to this code:
{
"CustomerAccount":{
"UserName":"String content"
};
I am trying to set the username here, does anyone know how can I set a name in dictionary with levels?
My code is as:
[mutabledictionary setObject:self.uitextfield.text forKey:UserName];
however this sets the username below to CustomerAccount not insider CustomerAccount.
Any help here would be appreciated.
Thanks.
If the two level mutable dictionary already exists (its not clear from your question) you need to send your setObject:forKey message to the second level dictionary, like so:
[[mutabledictionary objectForKey:#"CustomerAccount"] setObject:self.uitextfield.text forKey:UserName];
You can just add a sub-dictionary, like this:
NSDictionary *userAccount = [NSDictionary dictionaryWithObject:#"String Content" forKey:#"UserName"];
NSMutableArray *accounts = [NSMutableArray arrayWithObject:userAccount];
// do something with accounts
NSLog(#"%#", accounts);
Just create create array of CustomerAccount and use in NSDictionary. Use
[NSDictionary dictionaryWithObjects:objects forKeys:keys];
Related
I am trying to retrieve the value for a key in a NSDictionary. The dictionary has been initialised with key value pairs both of type string. The trouble is, I cannot seem to retrieve the value with I call objectForKey or valueForKey.
I am able to iterate over the dictionary and print both keys and values.
Can someone point out where im going wrong? Here is my code...
//Set up dictionary with options
keys = [NSArray arrayWithObjects:#"red", #"blue", nil];
values = [NSArray arrayWithObjects:#"1.7", #"2.8", nil];
conversionOptions = [NSDictionary dictionaryWithObject:values
forKey:keys];
Then this is called on the select row in a picker
NSLog(#"... %#", [keys objectAtIndex:row]); //prints out the key
NSString *theString = [keys objectAtIndex:row]; //save it as a string
NSLog(#"The string is... %#", theString); //print it out to make sure im not going crazy
NSLog(#"the value is : %#",[conversionOptions objectForKey:theString]); // I just get NULL here
//NSLog(#"the value is : %#",[conversionOptions valueForKey:theString]); // This doesn't work either, I just get NULL
You're creating a dictionary with an array as the only key and an array for its value! You want the dictionaryWithObjects:forKeys: (plural) factory, not dictionaryWithObject:forKey: (singular) so that each element of the keys array will be used as a distinct key for the respective element of the values array.
I find the dictionaryWithObjectsAndKeys: factory to be more useful most of the time, e.g.:
conversionOptions = [NSDictionary dictionaryWithObjectsAndKeys:#"1.7", #"red", #"2.8", #"blue", nil];
This is how you retrieve a string from a dictionary object
NSString * string = [dictionary objectForKey:#"stringKey"];
Use objectForKey: not valueForKey:.
Make sure conversionOptions is non-nil when you call objectForKey: (which is the right method).
And, yeah, derp -- I missed that the wrong factory method was used. You could do dictionaryWithObjects:andKeys: or do what pmjordan said said.
You absolutely must use objectForKey:, though. valueForKey: is specific to KVC.
I am making an app. where i want to add vendor name and it's device name..
I am using a dictionary where vendor name is key and device name are values..
Here is my code
[appDelegate.arrOfDevice addObject:txtDeviceName.text];
[appDelegate.dictOfDetails setObject:appDelegate.arrOfDevice forKey:txtVendorName.text ];
Here arrOfDevice is an array(declared in appdelegate) which is having all devices which are added..
I want to add devices based on particular keys...
I know, i am doing something wrong,
I am pushing the array as values in dictionary so it will store all the device names for each key.. but please help me...So that i could store device names based on particular key...
If u are not able to understand please fell free to get clarification of my question...
If you are adding key for the first time then you need to do this as shown below
if(flag==1) {
NSMutableArray *tmpArr=[[NSMutableArray alloc]init];
[tmpArr addObject:txtDeviceName.text];
[appDelegate.dictOfDetails setObject:tmpArr forKey:[txtVendorName.text uppercaseString]];
}
and if the key is present you just need to create a reference for an array and add object to the dictionary
else if(flag==0) {
NSMutableArray *arrDevice=[appDelegate.dictOfDetails objectForKey:[txtVendorName.text uppercaseString]];
[arrDevice addObject:txtDeviceName.text] ;
}
NSDictionary *dic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:#"Nit",[NSNumber numberWithInt:3],nil]
forKeys:[NSArray arrayWithObjects:#"Name",#"num",nil]];
>>Edited
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
#"value1", #"key1", #"value2", #"key2", nil];
also seem +dictionaryWithObjects:forKeys:
Hope, this will help you...
In my iPhone app, I am trying to use the JSON library (YAJL) to create a JSON string that looks like the following format:
{"user":
{"name":"Jon", "username":"jon22", "password":"passw#rd", "email":"jon22#example.com"}
}
But I can't figure out the YAJL methods to create this.
I have tried the following:
NSArray *params = [NSArray arrayWithObjects:#"Jon", #"jon22", #"passw#rd", #"jon22#gmail.com", nil];
NSArray *keys = [NSArray arrayWithObjects:#"name", #"username", #"password", #"email", nil];
NSDictionary *userDictionary = [NSDictionary dictionaryWithObjects:params forKeys:keys];
NSString *JSONString = [userDictionary yajl_JSONString];
However, the returned string is not wrapped in the outside "user".
How can I use YAJL to create this json string? Does anyone have experience with this???
Many thanks,
Brett
I don't use YAJL, but SBJSON, which is the one Apple uses in iOS too...
Anyway, the library is behaving correctly: you're not creating an "user" dictionary!
You need to do something like:
NSDictionary *serialize = [NSDictionary dictionaryWithObject:userDictionary forKey:#"user"];
And then "JSON-ize" this one.
This is because when you call -yajl_JSONString on userDictionary, you are using userDictionary as your "root object". If you want to wrap this inside another dictionary, you need to explicitly do so.
I highly recommend that you check out JSONKit https://github.com/johnezang/JSONKit I have used numerous JSON parsers and have found it to be the easiest.
Also the documentation is awesome.
I have a dictionary "playerDict" that reads data from a plist where there is names (myKey) with nine associated objects to each key.
I am trying to read all objects for a specific key (myKeys) into an NSMutableArray (theObjects). I have read the class reference and search internet but cannot figure our this, probably very simple, problem.
Among all other test i have done I have tried the following but that returns the key into theObjects and not the objects.
theObjects = [playerDict objectForKey:myKeys];
Anyone that could give a hint?
Here is the code that created the dict, i stripped it:
NSArray *objs = [NSArray arrayWithObjects:[NSNumber numberWithBool:playerObject.diffHard],[NSNumber numberWithBool:playerObject.diffMedium],
[NSNumber numberWithBool:playerObject.diffEasy],[NSNumber numberWithBool:playerObject.currentGame],
[NSNumber numberWithInt:playerObject.currentGameQuestion],[NSNumber numberWithInt:playerObject.currentGameRightAnswer],
[NSNumber numberWithInt:playerObject.currentGameType],[NSNumber numberWithInt:playerObject.nrOfType0Games],
[NSNumber numberWithInt:playerObject.type0Result], nil];
NSDictionary *newPlayerDict = [NSDictionary dictionaryWithObjectsAndKeys:objs, keyString, nil];
Try valueForKey:
You can only store one item per key in an NSDictionary. If you need story multiple items for the same key, you need to first add each of the items to an NSArray (or NSSet) that you instead then set as an object in your dictionary.
If might be useful if you posted the code that creates the dictionary.
Update: It looks like you are already doing this. So:
NSArray *myObjs=[playerDict objectForKey:keyString];
will get you your array. And this:
BOOL diffHard=[[myObjs objectAtIndex:0] boolValue];
BOOL diffMedium=[[myObjs objectAtIndex:1] boolValue];
Will get you the value you stored in the first and second objects of the array. Repeat it for the rest.
I have an NSMutableDictionary.
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
I have to update an element in that dictionary. How i can do that?
Please refer to the API here
First retrieve the objects using
[dict objectForKey:#"key"];
Later, you can set them back using:
- (void)setObject:(id)anObject forKey:(id)aKey
- (void)setValue:(id)value forKey:(NSString *)key
dictionary only allows you to have one objects for each key so if you use "set object for key" it will update the current one
NSMutableDictionary's method addEntriesFromDictionary.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableDictionary_Class/Reference/Reference.html
If both dictionaries contain the same key, the receiving dictionary’s previous value object for that key is sent a release message, and the new value object takes its place.