I want to add multiple value in NSDictionary. They make me trouble.
I am newer in Swift.
m=NSDictionary(objectsAndKeys:[self.getStringAt(selectStmt, column: 0)],"first",[self.getStringAt(selectStmt, column: 1)],"second",nil)
You can do it like this.
let interestingNumbers = [
"Prime" : [2,3,5,7,11,13],
"Fibonacci" : [1,1,2,3,5,8],
"Square" : [1,4,9,16,25]
]
Hope this helps.
/* Collection Types: Dictionary */
var dic1 = ["math":89, "chemistry":90, "physics":79]
// Modifying
dic1["math"]
dic1["math"] = 70 // returns new value
NSDictionary *eventLocation = [NSDictionary dictionaryWithObjectsAndKeys:#"43.93838383",#"latitude",#"-3.46",#"latitude" nil];
NSMutableDictionary *eventData = [NSDictionary dictionaryWithObjectsAndKeys:eventLocation,#"eventLocation", nil];
[eventData setObject:#"Jun 13, 2012 12:00:00 AM" forKey:#"eventDate"];
[eventData setObject:#"hjhj" forKey:#"text"];
.
.
.
NSMutableDictionary *finalDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:eventData,#"eventData", nil];
[finalDictionary setObject:#"ELDIARIOMONTANES" forKey:#"type"];
[finalDictionary setObject:#"accIDENTE" forKey:#"title"];
Related
Want to merge two NSDictionary or NSMutableDictionary only with dict1 values. like [NSMutableDictionary addEntriesFromDictionary:] and intersectSet (from NSMutableSet).
Dict 1 = #{#”One”: #””, #”Two”: #”"}
Dict 2 = #{#”One”: #”1”, #”Two”: 2, #”Three”: #”3"}
My expect output after merge = #{#”One”: #”1”, #”Two”: #”2"}
UPDATE
I am already tried with [Dict1 addEntriesFromDictionary:Dict2] this one give answer #{#”One”: #”1”, #”Two”: 2, #”Three”: #”3"}
Also I am tried with NSMutableSet
NSMutableSet *keysInA = [NSMutableSet setWithArray:tempDict.allKeys];
NSSet *keysInB = [NSSet setWithArray:tempDict1.allKeys];
[keysInA intersectSet:keysInB];
NSLog(#"keys in A that are not in B: %#", keysInA);
This one give output One, Two, Three.
So [NSMutableSet setWithArray:] and [NSMutableDictionary addEntriesFromDictionary:] won't give what I expected.
If above code have anything work let clear me. Thanks.
NSDictionary *dict1 = #{#"One": #"", #"Two": #"6"};
NSDictionary *dict2 = #{#"One": #"1", #"Two": #"2", #"Three": #"3"};
NSDictionary * dict = [dict2 dictionaryWithValuesForKeys:[dict1 allKeys]];
NSLog(#"dict %#",dict);
// Log value
dict {
One = 1;
Two = 2;
}
Here what I expected.
try this...
//Assuming both dictionaries have same kind of keys
NSMutableDictionary *dict1 = #{#"One" : #"", #"Two": #"", #"Three" : #""}.mutableCopy;
NSDictionary *dict2 = #{#"One" : #"1", #"Two": #"2", #"Three" : #"3",#"Four":#"4"};
for (NSString *key in dict1.allKeys)
{
if (dict2[key])
{
dict1[key] = dict2[key];
}
}
NSLog(#"Updated dict %#",dict1);
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;
}
)
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]
I am not sure if what I am going to ask is actually an NSDictionary with multiple keys but ok.
What I want to do is create an NSDictionary with keys and values for my data and then convert it to JSON format. The JSON format would look exactly like this :
{
"eventData": {
"eventDate": "Jun 13, 2012 12:00:00 AM",
"eventLocation": {
"latitude": 43.93838383,
"longitude": -3.46
},
"text": "hjhj",
"imageData": "raw data",
"imageFormat": "JPEG",
"expirationTime": 1339538400000
},
"type": "ELDIARIOMONTANES",
"title": "accIDENTE"
}
I ve only used NSDictionaries like this :
NSArray *keys = [NSArray arrayWithObjects:#"eventDate", #"eventLocation", #"latitude" nil];
NSArray *objects = [NSArray arrayWithObjects:#"object1", #"object2", #"object3", nil];
dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
But the above format is not all about key - value.
So my question is how would the NSDictionary be , to fit the JSON format??
Thanks for reading my post , and sorry if any confusion.
You can have a NSDictionary inside another NSDictonary:
NSDictionary *eventLocation = [NSDictionary dictionaryWithObjectsAndKeys:#"43.93838383",#"latitude",#"-3.46",#"latitude", nil];
NSMutableDictionary *eventData = [NSDictionary dictionaryWithObjectsAndKeys:eventLocation,#"eventLocation", nil];
[eventData setObject:#"Jun 13, 2012 12:00:00 AM" forKey:#"eventDate"];
[eventData setObject:#"hjhj" forKey:#"text"];
.
.
.
NSMutableDictionary *finalDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:eventData,#"eventData", nil];
[finalDictionary setObject:#"ELDIARIOMONTANES" forKey:#"type"];
[finalDictionary setObject:#"accIDENTE" forKey:#"title"];
Now with Objective-C literals there is a much better, easier, and cleaner way of accomplishing this. Here is your exact dictionary with this new syntax:
NSDictionary *dictionary = #{
#"eventData": #{
#"eventDate": #"Jun 13, 2012 12:00:00 AM",
#"eventLocation": #{
#"latitude": #43.93838383,
#"longitude": #-3.46
},
#"text": #"hjhj",
#"imageData": #"raw data",
#"imageFormat": #"JPEG",
#"expirationTime": #1339538400000
},
#"type": #"ELDIARIOMONTANES",
#"title": #"accIDENTE"
};
// Prints: "43.93838383"
NSLog(#"%#", dictionary[#"eventData"][#"eventLocation"][#"latitude"]);
How to Create NSArray and with Access for object using NSDictionary ?
... Create NSArray
NSArray *studentkeys = [NSArray arrayWithObjects:#"studentName", #"studentBirthDate", #"studentCity", #"studentMobile" nil];
NSArray *objects = [NSArray arrayWithObjects:#"Pravin", #"27/08/1990", #"Bhavnagar",#"7878007531", nil];
...to Access to NSArray Object Using NSDictionary
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
Here is the structure:
Your root object is NSMutableDictionary
eventData - key for object NSMutableDictionary with keys and objects:
->key eventDate object NSString
->key eventLocation object NSMutableDictionary with keys and objects:
----> key latitude object NSNumber
----> key longitude object NSNumber
-> key text object NSString
-> key imageData object NSString later converted to NSData
-> key imageFormat object NSString
-> key expirationTime object NSNumber
type key for object NSString
title key for object NSString
if you want multiple categories , you can follow this format
NSDictionary *jsonObject = #{
#"data1":#[
#{
#"title":#"A"
#"subData" : #[
#{
#"title":#"aa"
}]
}
]
};
I am creating an application in that I need to send a JSON to the server to get some response.
How to generate JSON using JSON Framework for iPhone?
What are the other possible ways?
Create an array or dictionary of objects representing the information you want to send via JSON. Having done that, send -JSONRepresentation to the array/dictionary. That method returns a JSON string, and you send it to the server.
For instance:
NSDictionary *o1 = [NSDictionary dictionaryWithObjectsAndKeys:
#"some value", #"key1",
#"another value", #"key2",
nil];
NSDictionary *o2 = [NSDictionary dictionaryWithObjectsAndKeys:
#"yet another value", #"key1",
#"some other value", #"key2",
nil];
NSArray *array = [NSArray arrayWithObjects:o1, o2, nil];
NSString *jsonString = [array JSONRepresentation];
// send jsonString to the server
After executing the code above, jsonString contains:
[
{
"key1": "some value",
"key2": "another value"
},
{
"key1": "yet another value",
"key2": "some other value"
}
]
Create an NSMutableDictionary or NSMutableArray and populate it with NSNumbers and NSStrings. Call [<myObject> JSONRepresentation] to return a JSON string.
eg:
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:#"Sam" forKey:#"name"];
[dict setObject:[NSNumber numberWithInt:50000] forKey:#"reputation"];
NSString *jsonString = [dict JSONRepresentation];