Adding arrays as keys and values in NSDictionary/NSMutableDictionary - ios

I have already initialized two arrays named - questionArray and correctAnswerArray.
I want to add these two arrays in a NSDictionary/ NSMutableDictionary (the one more appropriate) such that the questionArray will be the value (each index of it) and the correctAnswerArray will be the key.

Whether you chose NSMutableDictionary or not depends on if you want to change the data later on. Either way you can just change the following if you want a standard dictionary.
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjects:questionArray forKeys:correctAnswerArray];
** EDIT **
To retrive a value just do:
NSString *value = [dict valueForKey:#"KEY"];
or
NSString *value = dict[#"KEY"];

Related

How to add two array's objects as objects and keys to the third array in Objective C

I am trying to add two arrays into the third array i.e one array's objects are values to the third array and other array's objects are keys to the third array and I am getting the output as null , Is this a right way to do.
NSArray *newcontactkeys,*newcontactvalues ;
NSMutableArray *autoSyncDataArray;
newcontactkeys=[self.resultDict objectForKey:#"keys"];
newcontactvalues=[self.resultDict objectForKey:#"values"];
[autoSyncDataArray setValue:[self.resultDict objectForKey:#"values"]
forKey:[self.resultDict objectForKey:#"Keys"]];
NSLog(#"autosyncArray is %#",autoSyncDataArray);
Output: autosyncArray is (null)
You are using an array, which is an indexed collection, but talking about keys, dictionaries are keyed collections.
NSDictionary has a class method to directly create a dictionary from two arrays, use this and your code becomes, updating to modern Obj-C syntax:
NSArray *newContactKeys = self.resultDict[#"keys"];
NSArray *newContactValues = self.resultDict[#"values"];
NSDictionary *autoSyncDataDict = [NSDictionary dictionaryWithObjects:newContactValues
forKeys:newContactsKeys];
HTH
You're not initialising your array.
NSMutableArray *autoSynchDataArray = [NSMutableArray new]
Also use addObject instead of set value:
[autoSyncDataArray addObject:[self.resultDict objectForKey:#"values"]
You cannot join/Manipulate multiple Array of type NSArray.
Create a NSMutableArray which allows reordering and manipulation of data in it. You can add any number of objects to it, rearrange it and delete it.
NSMutableArray *templateArray = [NSMutableArray new];
There are 2 issues:
Null issue: You are not initializing array. So you will have to use below statement:
NSMutableArray *autoSyncDataArray = [[NSMutableArray alloc] init];
Key/Value Pair: If you want to put keys and values in an array. It will not work like Dictionary. It will go into sequential form. If you are ok with sequential form then use:
[autoSyncDataArray addObject:[self.resultDict objectForKey:#"keys"]];
[autoSyncDataArray addObject:[self.resultDict objectForKey:#"values"]];
otherwise use:
NSMutableDictionary *autoSyncDataDict = [[NSMutableDictionary alloc] init];
[autoSyncDataArray setValue:[self.resultDict objectForKey:#"values"]
forKey:[self.resultDict objectForKey:#"Keys"]];

I want to store NSString data in NSMutableDictionary

I'm trying to store data in NSMutableDictionary. continue change NSString value I want to store this value in NSMutableDictionary, but when second time store value in NSMutableDictionary then clear old value so how to store one by one value in NSMutableDictionary.
NSString *tempitemname = itemselectedlbl.text;
NSString *temprate = ratelbl.text;
NSString *tempquant = quantitylbl.text;
NSString *temptotal = totallbl.text;
NSString *temptotalbill = totalbilllbl.text;
dict = [[NSMutableDictionary alloc]init];
[dict setValue:tempitemname forKey:#"itemname"];
[dict setValue:temprate forKey:#"rate"];
[dict setValue:tempquant forKey:#"quantity"];
[dict setValue:temptotal forKey:#"total"];
[dict setValue:temptotalbill forKey:#"totalbill"];
Continue change all labels value I want store these values in NSMutableDictionary.
NSDictionary/NSMutableDictionary doesn't allow duplicate key to store inside them. So when you set the values second time then it overwrites the data if there is already a key available inside the dictionary. So you can't store again and again and still expect it to hold all the data.

How to read a plist and create different arrays from its content in Xcode

I've got a .plist like this:
Click image for full resolution
How can I create different arrays for each subpart of items. like NSArray foodName with contents of item 0's item 1's to item n's foodName
There are lots of items.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
self.pFile = [[paths objectAtIndex:0]
stringByAppendingPathComponent:#"recipes.plist"];
self.plist = [[NSMutableDictionary alloc] initWithContentsOfFile:pFile];
if (!plist) {
self.plist = [NSMutableDictionary new];
[plist writeToFile:pFile atomically:YES];
}
An NSDictionary is probably what you want. Then do a 'for' on it for each recipe / recipeDetail / wherever you're at in the structure?
You don't need to create "different arrays".
A plist is a textual (or binary) representation of a collection of objects. The valid object kinds come from a small set which includes NSArray, NSDictionary, NSNumber & NSString. The collection is rooted either in a dictionary or an array, each element of which can be any of the valid object kinds, including further dictionaries and arrays.
When you read the plist in your application the collection of objects is re-created. So if there are nested arrays or dictionaries they are recreated. To access them you just need to use the appropriate sequence of indices (arrays) and/or keys (dictionaries) which specify the element you need. You can store the returned object reference into a variable.
So for example if your plist is a dictionary keyed by vegetable names, each element of which is an array, and the third element of that array is another array of observed weights of that vegetable then you can access that array as follows (code just typed into answer, expect errors):
NSDictionary *vegetableInfo = [NSDictionary dictionaryWithContentsofURL:urlOfVegtableInfoPlist"];
NSArray *carrrotObservedWeights = vegetableInfo[#"Carrot"][3];
You now have a reference to the required array stored in carrrotObservedWeights.
If you are concerned over memory management, first use ARC. Second if you just want the extracted arrays and not the whole plist to be kept you just need to drop the reference to the plist after you've stored strong references to the contained arrays and ARC will clean up for you.
HTH
Addendum - After question clarified
First your plist is a dictionary of dictionaries where the containing dictionary has the keys Item 1, Item 2, etc. These keys carry no information and you would be better off making your plist an array of dictionaries.
Next we assume you have read in your plist using one of the standard methods and have a reference to it in sample - which is either an NSDictionary * if your plist is as shown, or an NSArray * if you modify the plist as suggested.
How many ways to do this? Many, here are three.
Method 1 - Simple Iteration
The straightforward way to obtain your arrays is simple iteration - iterate over each item and build your arrays. Here is a code fragment for two of the fields assuming the original dictionary of dictionaries:
NSMutableArray *foodNames = [NSMutableArray new];
NSMutableArray *hardnesses = [NSMutableArray new];
for (NSString *itemKey in sample) // for-in on a dictionary returns the keys
{
NSDictionary *item = sample[itemKey]; // use the key to obtain the contained dictionary
[foodNames addObject:item[#"foodName"]]; // extract each item
[hardnesses addObject:item[#"hardness"]];
}
The above fragment if sample is an array is similar.
Method 2 - Keypaths
If you do switch your plist to an array you can use a keypath to obtain all the values in one go. The method valueforkeypath: creates an array from an array by extracting the keypath - a list of keys separated by dots which allows for dictionaries within dictionaries. In your case the keypath has just one item:
NSMutableArray *foodNames = [sample valueForKeyPath:#"foodName"];
NSMutableArray *hardnesses = [sample valueForKeyPath:#"hardness"];
This will not work for your plist as shown with a top-level dictionary as they keypath is different each time - Item 1.foodName, Item 2.foodName, etc. - and wildcards (e.g. *.foodName) are not supported. A good reason to change your plist to have a top-level array!
Method 3 - Encapsulated Iteration
This is just a variation of method 1, but shows how to use the supplied block enumeration methods on NSDictionary. Rather than write a for loop yourself you can pass the body of the loop as a block to enumerateKeysAndObjectsUsingBlock: and it will perform the iteration:
NSMutableArray *foodNames = [NSMutableArray new];
NSMutableArray *hardnesses = [NSMutableArray new];
[sample enumerateKeysAndObjectsUsingBlock:^(id key, id item, BOOL *stop)
{
[foodNames addObject:item[#"foodName"]];
[hardnesses addObject:item[#"hardness"]];
}];
I'd make the root of the plist an array, but still have each item as a dictionary, so i could do something like this:
//ASSUMING YOUR PLIST IS IN RESOURCES FOLDER
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"YOUR_PLIST_FILE_NAME" ofType:#"plist"];
NSArray *foodArray = [NSArray arrayWithContentsOfFile:filepath];
NSMutableArray *foodNames = [[NSMutable Array] init];
NSInteger i = 0;
for (Item *item in foodArray) {
NSDictionary *itemDict = [foodArray objectAtIndex:i];
NSString *foodName = [itemDict objectForKey:#"foodName"];
[foodNames addObject:foodName];
i++;
}
Hope this helps you figure something out!

Keep NSArray original order in NSCountedSet

I have an NSArray and I need to get data from two keys and put together in a NSMutableDictionary. One key has stringvalues and the other NSNumbervalues. When I try to create NSCountedSetwithout adding the keys I want to use to separate arrays, it doesn't work, because the objects are not identical, basically, I need to check if objectId is identical, don't matter if the other keys are different.
Here is the initial code:
for (PFObject *objeto in objects) {
PFObject *exercicio = objeto[#"exercicio"];
NSString *string = exercicio.objectId;
NSNumber *nota = objeto[#"nota"];
[exercicios addObject:string];
[notas addObject:nota];
So I create two NSMutableArraysand store the values I need. When I logthe arrays after this, they are perfectly ordered, meaning the NSStringis in the same indexof the NSNumberit belongs to in the other array. So far, so good.
Now, when I create the NSCountedSetwith the strings, it changes the order.
NSCountedSet *countedExercicios = [[NSCountedSet alloc] initWithArray:exercicios];.
My goal is to sum the NSNumbers pertaining to an specific object, therefore, when the order changes, I lose the connection between the two arrays.
I'm not sure what I could do to solve this problem, or even if there's a different approach to achieve the result I need.
You can create NSDictionary and add it to array. You will have just one array and you won't lose the connection, you can use objectId as a key and NSNumber as a value:
for (PFObject *objeto in objects) {
PFObject *exercicio = objeto[#"exercicio"];
NSString *string = exercicio.objectId;
NSNumber *nota = objeto[#"nota"];
NSDictionary *dict = #{string: nota};
[newArray addObject: dict];
}
When you need get all key (objectId) you can use NSPredictate.
Hope this help

What's the standard convention for creating a new NSArray from an existing NSArray?

Let's say I have an NSArray of NSDictionaries that is 10 elements long. I want to create a second NSArray with the values for a single key on each dictionary. The best way I can figure to do this is:
NSMutableArray *nameArray = [[NSMutableArray alloc] initWithCapacity:[array count]];
for (NSDictionary *p in array) {
[nameArray addObject:[p objectForKey:#"name"]];
}
self.my_new_array = array;
[array release];
[nameArray release];
}
But in theory, I should be able to get away with not using a mutable array and using a counter in conjunction with [nameArray addObjectAtIndex:count], because the new list should be exactly as long as the old list. Please note that I am NOT trying to filter for a subset of the original array, but make a new array with exactly the same number of elements, just with values dredged up from the some arbitrary attribute of each element in the array.
In python one could solve this problem like this:
new_list = [p['name'] for p in old_list]
or if you were a masochist, like this:
new_list = map(lambda p: p['name'], old_list)
Having to be slightly more explicit in objective-c makes me wonder if there is an accepted common way of handling these situations.
In this particular case Cocoa is not outdone in succinctness :)
NSArray *newArray = [array valueForKey:#"name"];
From the NSArray documentation:
valueForKey:
Returns an array containing the
results of invoking valueForKey: using
key on each of the receiver's objects.

Resources