UPDATE
Whenever I comment out these lines writeToFile will create a file but if I dont,it will not work.
Here is my code..
NSMutableDictionary *allData = [[NSMutableDictionary alloc] init];
NSArray *countries = [self retrieveCountries];
for(NSDictionary *dicCountry in countries){
NSString *countryName = [dicCountry objectForKey:#"en_name"];
NSArray *capital = [self retrieveCapitals:countryName];
NSMutableDictionary *capitalInfo = [[NSMutableDictionary alloc] init];
for(NSDictionary *dictPerCap in capital){
NSString *cap = [dictPerCap objectForKey:#"en_name"];
NSArray *items = [self retrieveItems:cap];
NSArray *categories = [self retrieveCategories:cap];
if(items == nil)
items = [[NSArray alloc] init];
if(categories == nil)
categories = [[NSArray alloc] init];
// store data in a dictionary
NSDictionary* tempCapDic = [NSDictionary dictionaryWithObjectsAndKeys:
items, #"items",
categories, #"categories",
nil];
//store
[capitalInfo setObject:tempCapDic forKey:cap];
}
// store data in a dictionary
NSDictionary* dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
capital, #"capitals",
capitalInfo, #"info",
nil];
//store
[allData setObject:dataDic forKey:countryName];
}
/**************************/
NSDictionary *rootDict = [NSDictionary dictionaryWithObjectsAndKeys:countries, #"detailedCountries", allData, #"all", nil];
The lines that needs to be commented out inorder to work are:
capital, #"capitals",
capitalInfo, #"info",
in NSDictionary* tempCapDic.
Any idea on how to fix this?
Related
I have an NSMutableArray that contains several NSMutableDictionary object, each dict has a NSString and an NSMutableArray in it.
My problem is that I need to find the correct dict based on a string match and when found insert an object into the NSMutableArray in the same dict where the string match was found, I can only make this work the first time when the array is empty because after that I am unable to match the correct string:
Here is what I have tried so far, I tried using contains object but that wont get me inside the dict inside the array so I am stuck
NSMutableArray *variantOptions = [[NSMutableArray alloc] init];
for (NSDictionary *variant in variantInfo) {
NSMutableDictionary *variantOption = [[NSMutableDictionary alloc] init];
NSMutableArray *variantOptionValues = [[NSMutableArray alloc] init];
NSString *name = variant[#"name"];
NSString *value = variant[#"value"];
if(variantOptions.count > 0) {
// Need to loop through the array until name isEquaToString variantOptionName and when true insert value into variantOptionValuesArray in that same dict and if not exists create a new dict and insert in array
} else {
[variantOptionValues addObject:value];
[variantOption setObject:name forKey:#"variantOptionName"];
[variantOption setObject:variantOptionValues forKey:#"variantOptionValues"];
}
[variantOptions addObject:variantOption];
}
for (NSDictionary *variant in variantInfo) {
NSString *name = variant[#"name"];
NSString *value = variant[#"value"];
BOOL found = false;
for (NSMutableDictionary *v in variantOptions) {
if (v[#"name"] isequalToString:name]) {
NSMutableArray *values = v[#"variantOptionValues"];
[values addObject:value];
found = true;
break;
}
}
if (!found) {
// name was not found
NSMutableDictionary *variantOption = [[NSMutableDictionary alloc] init];
NSMutableArray *variantOptionValues = [NSMutableArray alloc] init];
[variantOptionValues addObject:value];
[variantOption setObject:name forKey:#"variantOptionName"];
[variantOption setObject:variantOptionValues forKey:#"variantOptionValues"];
[variantOptions addObject:variantOption];
}
}
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:#{#"Name": self.txt1.text , #"Roll No.":self.txt2.text,}];
NSMutableDictionary *dict1= [NSMutableDictionary dictionaryWithDictionary:#{#"Name":self.txt3.text , #"Roll No.":self.txt4.text}];
NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithDictionary:#{#"Name":self.txt5.text , #"Roll No.":self.txt6.text}];
NSMutableDictionary *dict3 = [NSMutableDictionary dictionaryWithDictionary:#{#"Name":self.txt7.text, #"Roll No.":self.txt8.text}];
How do i add these dictionary in single array...
Add objects(dictionaries) in array as following:-
NSArray *arrayData = [[NSArray alloc]initWithObjects:dict,dict1,dict2,dict3, nil];
//Show values in other view controller (as per comment)NSDictionary :-
Traverse the array, get the values, show the values in labels(IBOutlets)
*dicData = [arrayData objectAtIndex:0];
NSString *name = [dicData objectForKey:#"Name"];
NSString *rollNo = [dicData objectForKey:#"Roll No."];
labelName.text = name;
labelRollNo.text = rollNo;
attributes = [[NSMutableArray alloc] init];
[attributes addObject: dict.mutableCopy];
[attributes addObject: dict1.mutableCopy];
[attributes addObject: dict2.mutableCopy];
[attributes addObject: dict3.mutableCopy];
that's it
Try this
NSMutableArray *arrDict = [NSMutableArray alloc]initWithObjects:#{#"Name" : self.txt1.text , #"Roll No" : self.txt2.text},#{#"Name" : self.txt7.text, #"Roll No" : self.txt8.text} nil
Using NSArray Literal
NSArray *arrDict =#[#{#"Name" : self.txt1.text , #"Roll No" : self.txt2.text},#{#"Name" : self.txt7.text, #"Roll No" : self.txt8.text} ];
In Next ViewController you can get this dictionary through loop
for (NSDictionary *dicUser in arrDict) {
// this dictionary which contained by arrDict
UILabel *lblName = [dicUser objectForKey:#"Name"];
UILabel *lblRollNumber = [dicUser objectForKey:#"Roll No"];
}
I'm trying to create dictionary to POST JSON data to server:
NSArray *keys = [NSArray arrayWithObjects:#"lat", #"lon", nil];
NSArray *values = [NSArray arrayWithObjects: orderClass.extraLat, orderClass.extraLon, nil];
NSDictionary *postDict = [NSDictionary dictionaryWithObjects:values forKeys:keys];
this gives me:
{
lat = (
"54.720746",
"54.719206",
"54.717466"
);
lon = (
"56.011108",
"56.008510",
"56.007031"
);
}
But the aim is to POST data from arrays in format:
[{"lat":"54.720746", "lon":"56.011108" },
{ "lat":"54.719206", "lon":"56.008510"},
{ "lat":"54.717466", "lon":"56.007031"}]
Need your help.
Thanks for paying attention!
As I said in the comment - you need to reverse the steps towards your goal. First dictionaries, then array. And you'll get what you want.
NSArray *keys = [NSArray arrayWithObjects:#"lat", #"lon", nil];
NSArray *lats = [NSArray arrayWithObjects:#"1", #"2", #"3", nil];
NSArray *lons = [NSArray arrayWithObjects:#"4", #"5", #"6", nil];
// your way (not what you want)
NSArray *values = [NSArray arrayWithObjects: lats, lons, nil];
NSDictionary *postDict = [NSDictionary dictionaryWithObjects:values forKeys:keys];
// my recommendation based on what you want
NSMutableArray *postData = [[NSMutableArray alloc] init];
for (int i = 0; i < lats.count; i++) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[lats objectAtIndex:i] forKey:#"lat"];
[dict setObject:[lons objectAtIndex:i] forKey:#"lon"];
[postData addObject:dict];
}
This will get you these results:
(lldb) po postDict
{
lat = (
1,
2,
3
);
lon = (
4,
5,
6
);
}
And
(lldb) po postData
<__NSArrayM 0x7ffb5be4d950>(
{
lat = 1;
lon = 4;
},
{
lat = 2;
lon = 5;
},
{
lat = 3;
lon = 6;
}
)
I have no problems to get the value of name in terms (name of my blog post category) out of the xmlrpc server response object ...
Server response:
responseArray: (
{ guid = "http://www.domain.com/wp/?p=12";
"post_id" = "123";
terms = (
{ name = "Uncategorized"; }
);
}
)
... with the following lines of Objective-C code:
NSMutableArray *responseArray = [[NSMutableArray alloc] init];
responseArray = [nodeSaveResponse object];
for(i = 0; i < responseArray.count; i++) {
NSString *postLink = [[responseArray objectAtIndex: i] valueForKey: #"guid"];
NSString *postId = [[responseArray objectAtIndex: i] valueForKey: #"post_id"];
NSMutableArray *catArray = [[NSMutableArray alloc] init];
catArray = [[responseArray objectAtIndex: i] valueForKey: #"terms"]];
NSArray *cat = [catArray valueForKey: #"name"];
NSString *myCatString = [cat objectAtIndex: 0];
}
But to send a new blog post fails because the following code to pack the category string is somehow wrong:
NSString *myCatString = #"MyCategory";
NSMutableDictionary *name = [[NSMutableDictionary dictionaryWithObjectsAndKeys: myCatString, #"name", nil];
NSMutableArray *catArray = [[NSMutableArray arrayWithObject: name];
NSMutableDictionary *values = [[NSMutableDictionary alloc] init];
[values setObject: title forKey: #"post_title"];
// and so on with other values - until here everything works well
[values setObject: catArray forKey: #"terms"]; // if this line is called, the request fails
NSArray *params = [NSArray arrayWithObjects: #"1", user, pass, values, nil];
[myRequest setMethod: #"wp.newPost" withParameter: params];
Any idea, where my fault is?
Cheers, Martin
In my code, I set array inside a NSMutableDictionary as
if (array.count > 0) {
[self.filters setValue:array forKey:[self getKey:[[NSNumber numberWithInt:indexPath.section] intValue]]];
}
where array is
NSMutableArray *array = [[NSMutableArray alloc] init];
When the receiving code receives it, I tried to join the values in array as
if ([item objectForKey:#"category_filter"] != nil) {
NSArray *array = [NSArray arrayWithObjects:[item objectForKey:#"category_filter"], nil];
NSString *categories = [array componentsJoinedByString:#","];
NSLog(#"value:%#", categories);
}
where item is (NSMutableDictionary *)item
When I see log, I see as
2014-06-24 17:43:12.520 yelp[69744:70b] value:(
"Bagels (bagels)",
"Bakeries (bakeries)"
)
so they are not joined yet. What am I doing wrong here?
As Hot Licks commented,
I had to make change as
NSArray *array = [NSArray arrayWithArray:[item objectForKey:#"category_filter"]];
instead of
NSArray *array = [NSArray arrayWithObjects:[item objectForKey:#"category_filter"]];