I am revolving around with a small issue in iPhone while playing around Array and Dictionary. I have and product dictionary with the following data
name = Product;
options = (
{
code = code1;
name = "product AAA";
},
{
code = code1;
name = "product BBB";
},
{
code = "code2";
name = "product BBB";
},
{
code = "code3";
name = "product CCC";
},
{
code = "code3";
name = "product DDD";
},
{
code = code4;
name = "product EEE";
},
{
code = code4;
name = "product FFF";
}
);
Also i have an array of matching products
matchingProducts
{
"product BBB",
"product CCC",
"product DDD"
)
Now, all i want to do is i want to remove from products dictionary comparing to matchingProducts array. how can i do it.
Note: I cannot use key to remove objects as per my business rules. I have issue as i have names are repeated but i have to get the final result dictionary as shown below. Is it possible.
name = Product;
options = (
{
code = code1;
name = "product AAA";
},
{
code = code1;
name = "product BBB";
},
{
code = code4;
name = "product EEE";
},
{
code = code4;
name = "product FFF";
}
);
Please, reply me back if my question is not clear.
I have fixed the similar issue in java using the below code
for (int j = 0; j < matchingProducts.size(); j++) {
String product = ((Product) matchingProducts.elementAt(i)).name;
for (int i = 0; i <Product.size(); i++) {
String productName = ((Product) Product.elementAt(i)).name;
if (product.equals(productName)) {
Product.removeElementAt(i);
break;
}
}
}
NSMutableDictionary *Product = initialize like you did with Products;
NSMutableDictionary *resultant = [NSMutableDictionary alloc]init]; //this will have your result
NSArray *keys = [Product allKeys];
for(int i = 0 ; i < [keys count]; i++)
{
id temp = [Product objectForKey:keys[i]];
bool matchFound = NO;
for (int j = 0; j < [matchingProduct count]; j++)
{
id temptemp = [matchingProduct objectAtIndex:j] //Assuming matchingProduct is an NSArray or NSMutableArray
if(temp == temptemp)
{
matchFound = YES;
}
}
if(!matchFound)
[resultant addObject:temp];
}
// resultant now has what you wanted. Use it.
Related
Here I want to filter dictionary in the array which contains the "cate = subcat"
could anybody help me please
menuArr :(
{
Cate = subcat;
Description = "Grilled Chicken";
Id = 2;
Image = "http://asaraa.com/our_development/restaurant/admin/logo_image/large/28322grilled_chicken.jpg";
Name = "Grilled Chicken";
Price = 0;
Qty = "";
Title = "grilled chicken";
},
{
Cate = product;
Description = gravey;
Id = 12;
Image = "http://asaraa.com/our_development/restaurant/admin/logo_image/large/27166mutton.jpg";
Name = gravey;
Price = 50;
Qty = 1;
Title = gravey;
},
{
Cate = product;
Description = "Chicken Korma";
Id = 15;
Image = "http://asaraa.com/our_development/restaurant/admin/logo_image/large/77845Indian_chicken_recipes.jpg";
Name = "Chicken Korma";
Price = 99;
Qty = 1;
Title = "Chicken Korma";
},
{
Cate = subcat;
Description = "Chicken Sandwiches";
Id = 16;
Image = "http://asaraa.com/our_development/restaurant/admin/logo_image/large/67831chicken-sandwich-melt-0204_300.jpg";
Name = "Chicken Sandwiches";
Price = 0;
Qty = "";
Title = "Chicken Sandwiches";
}
)
NSString *searchString = #"subcat"
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"Cate = %#",searchString];
NSArray *array = [yourArray filteredArrayUsingPredicate:predicate];
if (array.count > 0)
{
// here goes your code
}
Using Linq to ObjectiveC you can filter it as follows:
NSArray* itemsInCategory = [menuArr where:^BOOL(id dic) {
return [[dic objectForKey:#"Cate"] isEqualToString:#"subcat"];
}];
This finds all the dictionaries where Cate=subcat.
Use NSPredicate
NSArray *filtered = [yourArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(Cate LIKE[CD] %#)", #"subcat"]];
My array contains three dictionaries per object of the array.
{
avg = {
avg1 = 50;
avg2 = 60;
};
posts = {
alcoholContent = 450;
name = "BBB";
origin = United States;
};
reviews = {
rev1 = "Test review 1";
rev2 = "Test review 2";
};
}
{
avg = {
avg1 = 30;
avg2 = 20;
};
posts = {
alcoholContent = 550;
name = "AAA";
origin = United States;
};
reviews = {
rev1 = "Test review 1";
rev2 = "Test review 2";
};
}
I want to sort array acceding by key "name" (of post dictionary).
How can I do it?
I tried normal sorting methods using sort descriptors, but did not work
Try sortUsingComparator:
[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDictionary *dict1 = obj1;
NSDictionary *dict2 = obj2;
NSString *string1 = [[dict1 objectForKey:#"posts"] objectForKey:#"name"];
NSString *string2 = [[dict2 objectForKey:#"posts"] objectForKey:#"name"];
return [string1 compare:string2];
}];
I'am able to display the data in nsdictionary using this code
NSString *testXMLString = #"<items><item id=\"0001\" type=\"donut\"><name>Cake</name><ppu>0.55</ppu><batters><batter id=\"1001\">Regular</batter><batter id=\"1002\">Chocolate</batter><batter id=\"1003\">Blueberry</batter></batters><topping id=\"5001\">None</topping><topping id=\"5002\">Glazed</topping><topping id=\"5005\">Sugar</topping></item></items>";
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:testXMLString error:&parseError];
NSLog(#"%#", xmlDictionary);
and the output is
items = {
item = {
batters = {
batter = (
{
id = 1001;
text = Regular;
},
{
id = 1002;
text = Chocolate;
},
{
id = 1003;
text = Blueberry;
}
);
};
id = 0001;
name = {
text = Cake;
};
ppu = {
text = "0.55";
};
topping = (
{
id = 5001;
text = None;
},
{
id = 5002;
text = Glazed;
},
{
id = 5005;
text = Sugar;
}
);
type = donut;
};
}; }
Can anyone please help me how to convert this dictionary into a xml file?
The NSPropertyListSerialization class should be able to do this for you.
Sorry for the long question but that best summarizes what I am trying to do:
My JSON Looks like:
{
4e8cf1d6c7e24c063e000000 = {
"_id" = {
"$id" = 4e8cf1d6c7e24c063e000000;
};
author = faisal;
comments = (
{
author = adias;
comment = amazing;
},
{
author = nike;
comment = "I concur";
}
);
created = {
sec = 1317772800;
usec = 0;
};
text = "This is a random post";
title = "post # 1";
type = (
punjabi
);
};
4e91fd49c7e24cda74000000 = {
"_id" = {
"$id" = 4e91fd49c7e24cda74000000;
};
author = draper;
comments = (
{
author = adias;
comment = "amazing again";
}
);
created = {
sec = 1318118400;
usec = 0;
};
text = "This is a random post again";
title = "post # 2";
type = (
punjabi
);
};
}
What I would like to do is to ultimately have a UTTableview with each row having a title (text from above JSON):
This is the code I have so far:
NSString *responseString = [request responseString];
NSDictionary *resultsDictionary = [responseString objectFromJSONString];
How do I put everything in an array for the UITableView? Again I am just a little rusty as I can swear I have done that before.
If you don't need the keys then you can convert it to an array by using allValues, e.g.:
NSArray *values = [resultsDictionary allValues];
Does your JSON really parse to an NSDictionary, or is this an NSArray containing NSDictionary instances? If the latter, then isn't it just something like:
NSString *textForCell = [[resultsArray objectAtIndex:row] valueForKey:#"title"];
I have a large NSDictionary that I need to loop through and create separate NSArrays. Here are the contents:
(
{
id = {
text = "";
};
sub = {
text = " , ";
};
text = "";
"thumb_url" = {
text = "";
};
title = {
text = "2010-2011";
};
type = {
text = "title";
};
},
{
id = {
text = "76773";
};
sub = {
text = "December 13, 2010";
};
text = "";
"thumb_url" = {
text = "http://www.puc.edu/__data/assets/image/0004/76774/varieties/thumb.jpg";
};
title = {
text = "College Days - Fall 2010";
};
type = {
text = "gallery";
};
},
{
id = {
text = "";
};
sub = {
text = "";
};
text = "";
"thumb_url" = {
text = "";
};
title = {
text = "2009-2010";
};
type = {
text = "title";
};
},
{
id = {
text = "76302";
};
sub = {
text = "December 3, 2010";
};
text = "";
"thumb_url" = {
text = "http://www.puc.edu/__data/assets/image/0019/76303/varieties/thumb.jpg";
};
title = {
text = "Christmas Colloquy";
};
type = {
text = "gallery";
};
}
)
Each section has a type key, which I need to check. When it finds the title key, I need to add those to an array. Then the next sections that would use the gallery key needs to be in its own array until it finds another title key. Then the gallery keys after that into their own array.
I am using a UITableView section titles and content. So, the NSDictionary above should have one NSArray *titles; array, and two other arrays each containing the galleries that came after the title.
I have tried using a for loop but I just can't seem to get it right. Any ideas would be appreciated.
It's somewhat unclear by your log, but I'm guessing your NSDictionary has values of NSDictionary? If so:
NSMutableArray *titles = [NSMutableArray array];
// etc.
for (id key in sourceDictionary) {
NSDictionary *subDictionary = [sourceDictionary objectForKey:key];
if ([subDictionary objectForKey:#"type"] == #"title")
[titles addObject:[subDictionary objectForKey:#"title"]];
// etc.
}
Your question is a bit unclear... but this is how you would properly loop through an NSDictionary.
EDIT:
NSMutableDictionary *galleries = [NSMutableDictionary dictionary];
NSString *currentTitle;
for (id key in sourceDictionary) {
NSDictionary *subDictionary = [sourceDictionary objectForKey:key];
NSString *type = [subDictionary objectForKey:#"type"];
if (type == #"title") {
currentTitle = [subDictionary objectForKey:#"title"];
if ([galleries objectForKey:currentTitle] == nil)
[galleries setObject:[NSMutableArray array] forKey:currentTitle];
} else if (type == #"gallery" && currentTitle != nil)
[[galleries objectForKey:currentTitle] addObject:subDictionary];
}
After this loop, galleries will contain keys of type NSString (with values of the titles), and corresponding objects of type NSArray (with values of the gallery NSDictionarys). Hopefully this is what you were going for.
NSString *key;
for(key in someDictionary){
NSLog(#"Key: %#, Value %#", key, [someDictionary objectForKey: key]);
}
Modern Objective-C syntax:
NSMutableArray *things = [NSMutableArray array];
NSMutableArray *stuff = [NSMutableArray array];
NSMutableArray *bits = [NSMutableArray array];
[dictionaries enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[things addObject:[obj valueForKeyPath:#"thing"]];
[stuff addObject:[obj valueForKeyPath:#"enclosing_object.stuff"]];
[bits addObject:[obj valueForKeyPath:#"bits"]];
}];