How to Convert NSMutableArray to NSMutableDictionary - ios

In objective c how to convert NSMutableArray to NSMutableDictionary I tried with this code.but only last index object only adding in the dictionary.
I need the format like this
ADDRESS =
{
major = 604;
minor = 37940;
uuid = "xxxxxxx";
};
{
major = 604;
minor = 37940;
uuid = "xxxxxxxxx";
};
{
major = 604;
minor = 37940;
uuid = "xxxxxxxxxx";
};
I tried with this code
NSMutableDictionary * dic1 = [NSMutableDictionary dictionary];
for (int j = 0; j <[self.beaconListArray count]; j ++)
{
[dic1 setObject:[self.beaconListArray objectAtIndex:j] forKey:#"ADDRESS"];
}

Try like this , Because you're dictionary is overiding object and you have already array of your data and you need to create dictionary using that for address key
NSMutableDictionary * dic1 = [NSMutableDictionary dictionary];
[dic1 setObject:self.beaconListArray forKey:#"ADDRESS"];

Use this.
NSMutableDictionary * dic1 = [NSMutableDictionary new];
dic1[#"ADDRESS"] = self.beaconListArray;

You can try to this code
Dictionary
NSMutableDictionary * dic1 = [NSMutableDictionary dictionary];
Add Dictionary to array with key
[dic1 setObject:self.beaconListArray forKey:#"ADDRESS"];
Print Dictionary
NSLog(#"dic1====: %#",dic1);

Related

how to get nsDictionary element by using for-in

NSDictionary *myDict = #{#"one":#"1",#"two":#"2"};
for (NSDictionary* tmp in myDict) {
NSLog(#"%#",tmp);
}
resut:
my tmpis NSString
I want to get a dictionary with key= one , value = 1
for in for NSDictionary will iterate the keys.
for (NSString * key in myDict) {
NSLog(#"%#",key);
NSString * value = [myDict objectForKey:key];
}
If you want to get a dictionary. You have to create a dictionary from these values
for (NSString * key in myDict) {
NSLog(#"%#",key);
NSString * value = [myDict objectForKey:key];
NSDictionary * dict = #{key:value};
}
Or you should init like this:
NSArray *arrDict = #[{#{"one":#"1"},#{#"two":#"2"}];
for (NSDictionary* tmp in arrDict) {
NSLog(#"%#",tmp);
}
You can get all keys from your dic then add the key and value to your new dic like this:
NSDictionary *myDict = #{#"one":#"1",#"two":#"2"};
NSArray *keys = [myDict allKeys];
for (NSString *key in keys) {
NSDictionary *yourDic = #{key: [myDict valueForKey:key]};
NSLog(#"%#", yourDic);
}
You didn't create it that way. If you wanted to have a NSDictionary inside another NSDictionary you should write something like this :
NSDictionary *myDict = #{
#"firstDict" : #{
#"one":#"1"
},
#"secondDict": #{
#"two":#"2"
}
};
Above code will create a NSDictionary with two dictionaries at keys #firstDict and #secondDict.
Also, bear in mind, that because dictionaries are key-value pairs, using a for-in loop, actually loops through the keys in that dictionary. So your code is equivalent to:
for(NSString *key in dict.allKeys) { ... }
I got the solution
NSDictionary *myDict = #{#"one":#"1",#"two":#"2"};
NSMutableArray *arrayObject = [[NSMutableArray alloc]init];
NSMutableArray *arrayKey = [[NSMutableArray alloc]init];
NSMutableArray *arrayObjectKey = [[NSMutableArray alloc]init];
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
for (NSString *stringValue in myDict.allValues)
{
[arrayObject addObject:stringValue];
}
for (NSString *stringKey in myDict.allKeys)
{
[arrayKey addObject:stringKey];
}
for(int i = 0;i<[arrayKey count];i++)
{
dict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:[NSString stringWithFormat:#"%#",[arrayKey objectAtIndex:i]],#"key",nil];
[dict setObject:[NSString stringWithFormat:#"%#",[arrayObject objectAtIndex:i]] forKey:#"value"];
[arrayObjectKey addObject:dict];
}
NSLog(#"The arrayObjectKey is - %#",arrayObjectKey);
The Output is
The arrayObjectKey is -
(
{
key = one;
value = 1;
},
{
key = two;
value = 2;
}
)
Create the dictionary:
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:#"1",#"One",#"2","Two",nil];
Get a value out using:(this example tmp will be 1)
NSString *tmp = [myDict objectForKey:#"One"];
Display the output in console:
NSLog(#"%#",tmp);
To display the whole NSDictionary
NSLog (#"contents of myDict: %#",myDict);
What you are doing is creating a dictionary with key-value pairs. I think what you want to do is have an array with dictionaries.
NSArray *myArray = #[#{#"one":#"1"}, #{#"two":#"2"}];
for (NSDictionary* tmp in myArray) {
NSLog(#"%#",tmp);
}
However I don't see a point in doing this. What you could do is:
NSDictionary *myDict = #{#"one":#"1",#"two":#"2"};
for (NSString* key in [myDict allKeys]) {
NSLog(#"%# = %#", key, myDict[key]);
}

NSDictonary set value for key in for loop Get last value only

I want to pass the parameters to server the parameters are in one array like
parameters1 (2,3,5,7,9,16,25) i want to pass
#"tags[]" : #"2"
#"tags[]" : #"4"
.........
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject: userId forKey:#"user_id"];
[parameters setObject: userToken forKey:#"token"];
for (i = 0 ; i < parameters1.count ; i++) {
[parameters setObject:[parameters1 objectAtIndex:i]forKey:#"tags[]"];
}
I am getting the output as
#"user_id" : 2
#"token" : khsskjkjwllwklkllk
#"tags[]" : 25
here i want to pass all the tags but i am getting only last tages value only
i want to add the tags from an parameters Dictonary.
A dictionary can only hold one value per key, so:
[parameters setObject:[parameters1 objectAtIndex:i]forKey:#"tags[]"];
Always sets parameters1[i] as the (string) value for #"tags[]".
Perhaps you intended to use an array as the value for that key? I can't say, but if so:
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
parameters[#"user_id"] = userId;
parameters[#"token"] = userToken;
parameters[#"tags[]"] = parameters1;
You can direct add array of parameters1 for "tag[]".
As NSDictionary can hold only one value for each key you have to differentiate your key with any extra number or character.
Here you are using key tags[] which can hold last set value.
I have update your code with one small change which can change your key and hold each value
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject: userId forKey:#"user_id"];
[parameters setObject: userToken forKey:#"token"];
for (i = 0 ; i < parameters1.count ; i++) {
[parameters setObject:[parameters1 objectAtIndex:i] forKey:[NSString stringWithFormat#"tags[%d]",i]];
}
Your will get output as
#"user_id" : 2
#"token" : khsskjkjwllwklkllk
#"tags[0]" : 2
#"tags[1]" : 3
#"tags[2]" : 5
#"tags[3]" : 7
#"tags[4]" : 9
#"tags[5]" : 16
#"tags[6]" : 25
I hope you understand the answer.
I just edited your code,
NSMutableArray *parameters1 = [[NSMutableArray alloc] initWithObjects:#"2",#"3",#"5",#"7",#"9",#"16",#"25", nil];
NSMutableDictionary *parameters= [NSMutableDictionary dictionary];
NSMutableDictionary *parameters2 = [NSMutableDictionary dictionary];
for (int i = 0 ; i < parameters1.count ; i++) {
NSMutableDictionary *parametersTag = [NSMutableDictionary dictionary];
[parametersTag setObject:[parameters1 objectAtIndex:i]forKey:#"tags[]"];
[parameters1 replaceObjectAtIndex:i withObject:parametersTag];
}
[parameters setObject: #"111" forKey:#"user_id"];
[parameters1 addObject:parameters];
[parameters2 setObject: #"dhfjkhds" forKey:#"token"];
[parameters1 addObject:parameters2];
NSLog(#"arr : %#",parameters1);
It will generate output as below,
(
{
"tags[]" = 2;
},
{
"tags[]" = 3;
},
{
"tags[]" = 5;
},
{
"tags[]" = 7;
},
{
"tags[]" = 9;
},
{
"tags[]" = 16;
},
{
"tags[]" = 25;
},
{
"user_id" = 111;
},
{
token = dhfjkhds;
}
)

xcode - set value of a nested object

I have a json from a service and I need to change the values of one obeject.
{
question = (
{
answer = (
{
Id = 1;
value = 1;
},
{
Id = 2;
value = 0;
}
);
},
.....
I use that code to directly access to the second "value" element and set it to "true"
NSMutableDictionary * dict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
NSMutableDictionary *preguntasDict = [[NSMutableDictionary alloc] init];
preguntasDict =[[[dict valueForKey:#"question"]mutableCopy];
NSMutableDictionary *answer = [[NSMutableDictionary alloc] init];
respuestasDict =[[[[preguntasDict valueForKey:#"answer"]objectAtIndex:0]objectAtIndex:1] mutableCopy];
[respuestasDict setObject:[NSNumber numberWithBool:true] forKey:#"value"];
It works: "respuestasDict" changes but the whoole "dict" not.
My question is: how could rebuild the entire dictionary? or it is possible to access directly to the nested object and change it?
Note that perguntasDict and respuestasDict are mutable copies of your dictionaries, so basically you are editing copies of your dict. You need to access dict directly, like this:
NSArray *answers = dict[#"question"][#"answer"];
[answers firstObject][#"value"] = #(YES);
PS: Doing dict[#"question"] is the same thing as [dict objectForKey:#"question"]. Also, #(YES) is the same thing as [NSNumber numberWithBool:true]

How to replace the value of the key in NSMutableDictionary based on the another key value pair

I have one NSArray with NSMutableDictionaries .Example testArray =[dict1,dict2,dict3,dict4].
Each dictionary in the array is something like
dict1= {
country = "INDIA";
flag = "A";
Currency = "Rupees";
rate = 10;
}
The all values are changeable. Sometimes
dict1 = {
country = "USA";
flag = "SA";
Currency = "Dollar";
rate = 50;
}
I need to update the value of the key rate of all dictionaries in the testArray if country = "INDIA" and flag = "A" and Currency = "Rupees" and Do not need to change the value of the rate key all other dictionaries in side the testArray.
Just try the below.
for(NSMutableDictionary *dic in array)
{
if([[dic valueForKey:#"country"] isEqualToString:#"INDIA"] &&
[[dic valueForKey:#"flag"] isEqualToString:#"A"] &&
[[dic valueForKey:#"Currency"] isEqualToString:#"Rupees"])
{
[dic setValue:#"100" forKey:#"rate"];
}
}
Here is a complete example what you need
NSDictionary *dict1 = #{
#"country" : #"INDIA",
#"flag" : #"A",
#"Currency" : #"Rupees",
#"rate" : #10
};
NSDictionary *dict2 = #{
#"country" : #"USA",
#"flag" : #"SA",
#"Currency" : #"Dollar",
#"rate" : #50
};
NSArray *testArray = #[dict1, dict2];
NSMutableArray *testMutableArray = [testArray mutableCopy];
for (int i = 0; i < testMutableArray.count; i++) {
NSDictionary *country = testMutableArray[i];
if ([country[#"country"] isEqualToString:#"INDIA"] &&
[country[#"flag"] isEqualToString:#"A"] &&
[country[#"Currency"] isEqualToString:#"Rupees"])
{
NSMutableDictionary *countryMutable = [country mutableCopy];
countryMutable[#"rate"] = #100;
testMutableArray[i] = [countryMutable copy];
}
}
testArray = [testMutableArray copy];
Use NSMutableArray instead of NSArray.
then,
NSDictionary *dict = [testArray objectAtIndex:0];
dict[#"rate"] = #"11";
[testArray replaceObjectAtIndex:0 withObject:dict];
Hope this will help you.

NSDictionary adding multiple keys

May I know is that possible for NSDictionary or NSMutableDictionary to add in multiple same keys for different object? It is because of the API that written by the developers are accepting an array.
e.g:
NSArray *ids = #[#"xxx", #"yyy", #"zzz"];
NSMutableDictionary *args = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"someobject", #"somekey"];
I've defined an args and set of ids that picked by user and now I will loop the array.
for( NSString *getId in ids ){
[args setObject:getId forKey:#"ids[]"];
}
So ended up, the results come out are
"somekey" = "someobject", "ids[]" = "zzz";
Is that possible for me to get result as follows?
"somekey" = "someobject", "ids[]" = "xxx", "ids[]" = "yyy", "ids[]" = "zzz";
Please advise, thanks!
Yes it is possible
NSArray *arr = [[NSArray alloc]initWithObjects:#"xxx",#"yyy",#"zzz", nil];
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"Someobject", #"Somekey", nil];
for (int i = 0; i<[arr count]; i++) {
[dic setValue:[arr objectAtIndex:i] forKey:[NSString stringWithFormat:#"id[%d]", i]];
}
NSLog(#"dic %#",dic);
Use this code sure it would help you.
No, key is unique. But you can put an NSMutableArray in your NSDictionary, and store you values like key => array(x,y,...)
Instead of that you can create Dictionary like this.
YourDictionary = {
"somekey" = "someobject",
"ids[]" = (
"xxx",
"yyy",
"zzz"
)
}
Treat ids[] as an array
Hope this will solve your problem
No that's not the way a dictionary should be used. As peko said, just put your array in the dict:
[args setObject:ids forKey:#"ids"];

Resources