using NSPredicate on nested dictionary/array - objective-c

I'm trying to use a deeply nested data structure and NSPredicate to filter that data.
I have a plist file like this:
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>images-ipad</key>
<array>
<dict>
<key>categories</key>
<array>
<string>standard</string>
</array>
<key>name</key>
<string>Default</string>
<key>plist</key>
<string>cardSheet.plist</string>
<key>png</key>
<string>cardSheet.png</string>
</dict>
<dict>
<key>categories</key>
<array>
<string>standard</string>
</array>
<key>name</key>
<string>Optional</string>
<key>plist</key>
<string>cardSheet.plist</string>
<key>png</key>
<string>cardSheet.png</string>
</dict>
<dict>
<key>categories</key>
<array>
<string>christmas</string>
<string>holiday</string>
<string>standard</string>
</array>
<key>name</key>
<string>christmas</string>
<key>plist</key>
<string>cardSheet.plist</string>
<key>png</key>
<string>cardSheet.png</string>
</dict>
</array>
</dict>
</plist>
Which I load into an NSDictionary via dictionWithContentsOfFile.
I then want to get the elements of the array images-ipad that are in the category 'standard' via NSPredicate. I think this is possible, but I've not been able to accomplish it, and the docs don't seem to cover nested data structures.
So, I've tried this:
NSPredicate *getcat = [NSPredicate predicateWithFormat:#"ANY images-ipad.categories == 'holiday'"];
NSArray *res = [[dict objectForKey:#"images-ipad"] filteredArrayUsingPredicate:getcat];
but it does not return any elements, which brings me to stackoverflow. Thanks in advance.

Since you're filtering the result of [dict objectForKey:#"images-ipad"], you shouldn't include that string in the key path of the predicate, as it's basically equivalent to looking up images-ipad.images-ipad.categories, which doesn't exist.

Related

Cannot Fetch more than one key arrays from my Info.plist file

i am trying to fetch two arrays against two keys in my Info.plist but instead not getting any data in my output. here is my code.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0),^{
NSString *post = [NSString stringWithFormat:#"URL"];
NSDictionary *dTmp = [[NSDictionary alloc] initWithContentsOfURL:[NSURL URLWithString:post]];
NSMutableArray *OriginalDetailsArray=[dTmp valueForKey:#"Objects"];
NSMutableArray *OriginalGalleryArray=[dTmp valueForKey:#"gallery"];
detailsArray=[[NSMutableArray alloc] init];
[detailsArray addObjectsFromArray:OriginalDetailsArray];
galleryArray=[[NSMutableArray alloc] init];
[galleryArray addObjectsFromArray:OriginalGalleryArray];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"%#, %#",detailsArray, galleryArray);
});
});
Out Put is: You can see there is nothing in the output just empty brackets.
(
), (
)
here is my Info.plist file. if i remove second array "Gallery" and i only let "Objects" data fetches perfectly. But after adding Second Array it gives empty output.
<plist version="1.0">
<dict>
<key>Objects</key>
<array>
<dict>
<key>propid</key>
<integer>233</integer>
<key>title</key>
<string>101 Perthshire Private</string>
<key>type</key>
<string>For Sale</string>
<key>price</key>
<string>$899,900</string>
<key>bed</key>
<string>2</string>
<key>bath</key>
<string>3</string>
</dict>
</array>
</dict>
<dict>
<key>gallery</key>
<array>
<dict>
<key>imageurl</key>
<string>
/images/23330.jpg
</string>
</dict>
<dict>
<key>imageurl</key>
<string>
/images/23333.jpg
</string>
</dict>
<dict>
<key>imageurl</key>
<string>
/images/23339.jpg
</string>
</dict>
</array>
</dict>
</plist>
Your plist data formot are wrong. Please put below code on your php file and check it.
<plist version="1.0">
<dict>
<key>Objects</key>
<array>
<dict>
<key>propid</key>
<integer>233</integer>
<key>title</key>
<string>101 Perthshire Private</string>
<key>type</key>
<string>For Sale</string>
<key>price</key>
<string>$899,900</string>
<key>bed</key>
<string>2</string>
<key>bath</key>
<string>3</string>
</dict>
</array>
<key>gallery</key>
<array>
<dict>
<key>imageurl</key>
<string>/images/23330.jpg</string>
</dict>
<dict>
<key>imageurl</key>
<string>/images/23333.jpg</string>
</dict>
<dict>
<key>imageurl</key>
<string>/images/23339.jpg
</string>
</dict>
</array>
</dict>
</plist>
Please Try Below Code, I got Result.
NSArray *arrayObjects = [NSArray arrayWithArray:[dictRoot objectForKey:#"Objects"]];
NSArray *arrayGallery = [NSArray arrayWithArray:[dictRoot objectForKey:#"gallery"]];
NSLog(#"%#",arrayObjects);
NSLog(#"%#",arrayGallery);

How to combine entries from two NSMutableDictionary objects?

i try and combine my dictionaries but to be honest i have no idea how. i try like thus, but two overtake one. sorry for the bad english.
[dict addEntriesFromDictionary:dict2];
1
<plist version="1.0">
<dict>
<key>Main</key>
<dict>
<key>DataStorage1</key>
<dict>
<key>A</key>
<string></string>
<key>B</key>
<string></string>
<key>C</key>
<string></string>
</dict>
</dict>
</dict>
</plist>
2
<plist version="1.0">
<dict>
<key>Main</key>
<dict>
<key>DataStorage2</key>
<dict>
<key>A</key>
<string></string>
<key>B</key>
<string></string>
<key>C</key>
<string></string>
</dict>
</dict>
</dict>
</plist>
I would like to make:
<plist version="1.0">
<dict>
<key>Main</key>
<dict>
<key>DataStorage1</key>
<dict>
<key>A</key>
<string></string>
<key>B</key>
<string></string>
<key>C</key>
<string></string>
</dict>
<key>DataStorage2</key>
<dict>
<key>A</key>
<string></string>
<key>B</key>
<string></string>
<key>C</key>
<string></string>
</dict>
</dict>
</dict>
I tried, but it never combined correctly.
Both dict and dict2 have one one key-value pair with the common key "Main".
Therefore
[dict addEntriesFromDictionary:dict2];
replaces the entire "Main" dictionary in dict with the value from dict2.
What you probably want is
[dict[#"Main"] addEntriesFromDictionary:dict2[#"Main"]];
to add the key-values pairs from the "Main" dictionary in dict2 to the "Main"
dictionary in dict. (This assumes that the "Main" dictionary in dict
is mutable.)
Example:
NSURL *url1 = [[NSBundle mainBundle] URLForResource:#"plist1" withExtension:#"plist"];
NSURL *url2 = [[NSBundle mainBundle] URLForResource:#"plist2" withExtension:#"plist"];
NSMutableDictionary *dict1 = [NSMutableDictionary dictionaryWithContentsOfURL:url1];
NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithContentsOfURL:url2];
[dict1[#"Main"] addEntriesFromDictionary:dict2[#"Main"]];

How to find the number of values in a plist?

I have a plist that looks like the following.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<!--
Translations
-->
<plist version="1.0">
<array>
<dict>
<key>English</key> <string>Hello Mrs./Mr./Miss.</string>
<key>French</key> <string>Bonjour Madame / Monsieur / Mademoiselle.</string>
<key>Spoken</key> <string>Boh(n)-zhoor mah-dahme/ muh-syuhr/ mah-dah-mwa-sell.</string>
</dict>
<dict>
<key>English</key> <string>Excuse me.</string>
<key>French</key> <string>Pardon.</string>
<key>Spoken</key> <string>Par-doh(n).</string>
</dict>
<dict>
<key>English</key> <string>Do you speak English?</string>
<key>French</key> <string>Parlez-vous anglais?</string>
<key>Spoken</key> <string>Par - lay vooz ah(n)-glay?</string>
</dict>
</array>
</plist>
I want to know how to find how many elements are in the plist. For example, there is 3 in the code above. Is there an attribute of plist I can call to find this number?
Read the plist into an array:
NSArray *plistArray = [NSArray arrayWithContentsOfFile:pathToPlistFile];
and count the number of elements:
NSUInteger count = [plistArray count];

Sum of records in property list

How can I calculate the sum of all numbers in all dictionaries for the key for example "Hours"?
Couldn't find any informations about that.
Here is piece of my property list I'm talking about:
<plist version="1.0">
<array>
<dict>
<key>Address1</key>
<string>17 Stanley Road</string>
<key>Address2</key>
<string>Essex</string>
<key>City</key>
<string>Southend On Sea</string>
<key>Email</key>
<string>lmozdzen#gmail.com</string>
<key>Lessons</key>
<array>
<dict>
<key>LessonComment</key>
<string></string>
<key>LessonDate</key>
<string>05/06/2013</string>
<key>LessonHomework</key>
<string></string>
<key>LessonHours</key>
<integer>1</integer>
<key>LessonNext</key>
<string></string>
<key>LessonNumber</key>
<string>1</string>
<key>LessonTime</key>
<string></string>
</dict>
<dict>
<key>LessonComment</key>
<string>Ryun</string>
<key>LessonDate</key>
<string></string>
<key>LessonHomework</key>
<string>T</string>
<key>LessonHours</key>
<integer>2</integer>
<key>LessonNext</key>
<string>Ty</string>
<key>LessonNumber</key>
<string>4</string>
<key>LessonTime</key>
<string></string>
</dict>
</array>
I want the sum of all numbers for key "LessonHours"
Using Key Value Coding:
NSNumber *sum = [[yourDict allKeys] valueForKeyPath:#"#sum.theIntegerPropertyToSum"];
Or you can use plane old style addition using loop.
NSInteger sum=0;
for(NSString *string in yourDict){
sum+=[string integerValue];
}

Parsing complex nest of NSDictionaries and Arrays to a simpler NSDictionary

I'm in a bit over my head here (a lot actually). I have a JSON response that's been converted to an NSDictionary. It's a mess of nested arrays and dictionaries within dictionaries within arrays, etc... I have no clue what to do with it. What I would like to do is make a simple NSDictionary for each "item" and use the properties to drive a UITableView. I don't know how to access objects several levels down. I've done a bit of searching and i see there's several ways to go about it (fast enumeration, blocks, etc..) but before I start spinning my wheels I'd love to know what would be best for my particular situation. I saved the NSDictionary as a plist for readability. I'll post it here:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>search_result</key>
<dict>
<key>latitude</key>
<string>51.508129</string>
<key>longitude</key>
<string>-0.128005</string>
<key>page</key>
<string>1</string>
<key>per_page</key>
<integer>3</integer>
<key>results</key>
<array>
<dict>
<key>result</key>
<dict>
<key>description_short</key>
<string>Experience the history and mystery of the most famous sites in the South of England!</string>
<key>flex_reference</key>
<string>FLX-LON-77B-D2F-5ED5</string>
<key>geocoded_latitude</key>
<string>51.5081289</string>
<key>geocoded_longitude</key>
<string>-0.128005</string>
<key>name</key>
<string>Stonehenge, Glastonbury, Avebury and Chalice Well</string>
<key>primary_image</key>
<string>http://media.****.com/FLX-LON-77B-D2F-5ED5-image_original-56.jpg</string>
<key>product_categories</key>
<array>
<dict>
<key>product_category</key>
<dict>
<key>kind</key>
<string>type</string>
<key>name</key>
<string>Sightseeing Tours</string>
</dict>
</dict>
<dict>
<key>product_category</key>
<dict>
<key>kind</key>
<string>category</string>
<key>name</key>
<string>Excursion</string>
</dict>
</dict>
<dict>
<key>product_category</key>
<dict>
<key>kind</key>
<string>type</string>
<key>name</key>
<string>Attractions</string>
</dict>
</dict>
</array>
</dict>
</dict>
<dict>
<key>result</key>
<dict>
<key>description_short</key>
<string>Experience some of the most famous landmarks in English history, University City of Oxford, rolling countryside and honey stoned cottages of the Cotswolds and Stratford upon Avon home of the famous English plyright, William Shakespeare.</string>
<key>flex_reference</key>
<string>FLX-LON-2AD-267-38AB</string>
<key>geocoded_latitude</key>
<string>51.5081289</string>
<key>geocoded_longitude</key>
<string>-0.128005</string>
<key>name</key>
<string>Oxford, Stratford and the Cotswolds Villages</string>
<key>primary_image</key>
<string>http://media.*****.com/FLX-LON-2AD-267-38AB-image_original-AF.jpg</string>
<key>product_categories</key>
<array>
<dict>
<key>product_category</key>
<dict>
<key>kind</key>
<string>type</string>
<key>name</key>
<string>Sightseeing Tours</string>
</dict>
</dict>
<dict>
<key>product_category</key>
<dict>
<key>kind</key>
<string>category</string>
<key>name</key>
<string>Excursion</string>
</dict>
</dict>
<dict>
<key>product_category</key>
<dict>
<key>kind</key>
<string>type</string>
<key>name</key>
<string>Attractions</string>
</dict>
</dict>
</array>
</dict>
</dict>
<dict>
<key>result</key>
<dict>
<key>description_short</key>
<string>The must see attractions of the United Kingdom, World Heritage sites, Stonehenge and the Roman Baths.</string>
<key>flex_reference</key>
<string>FLX-LON-65D-AC0-B08E</string>
<key>geocoded_latitude</key>
<string>51.5081289</string>
<key>geocoded_longitude</key>
<string>-0.128005</string>
<key>name</key>
<string>Stonehenge and Bath</string>
<key>primary_image</key>
<string>http://media.****.com/FLX-LON-65D-AC0-B08E-image_original-2E.jpg</string>
<key>product_categories</key>
<array>
<dict>
<key>product_category</key>
<dict>
<key>kind</key>
<string>type</string>
<key>name</key>
<string>Sightseeing Tours</string>
</dict>
</dict>
<dict>
<key>product_category</key>
<dict>
<key>kind</key>
<string>category</string>
<key>name</key>
<string>Excursion</string>
</dict>
</dict>
</array>
</dict>
</dict>
</array>
<key>total_pages</key>
<integer>18</integer>
<key>total_results</key>
<integer>54</integer>
</dict>
</dict>
</plist>
NSDictionary *jsonResponse = /* get your dictionary from wherever */
NSDictionary *searchResults = [jsonResponse objectForKey:#"search_result"];
NSArray *allResults = [searchResults objectForKey:#"results"];
for (NSDictionary *result in allResults)
{
NSDictionary *resultDetails = [result objectForKey:#"result"];
NSString *name = [resultDetails objectForKey:#"name"];
NSURL *imageURL = [NSURL URLWithString:[resultDetails objectForKey:#"primary_image"]];
NSLog(#"Name: %#\n URL: %#", name, imageURL);
}
Retrieve the dictionary:
NSDictionary* dict= jsonDict[#"search_results"];
The value for key "results" is an array:
NSArray* results = dict[#"results"];
At the first index there is a dictionary:
NSDictionary* dict2= results[0];
For the key "result" there is another dictionary:
NSDictionary* dict3= dict2[#"result"];
From this dictionary you can retrieve the values that you want:
NSString* name= dict3[#"name"];
NSString* primaryImage= dict3[#"primary_image"];
No matter how many dicts and arrays you have, you can read one then you can read them all.To do this easier just think like they' re packets inside packets, and draw a graph with the objects.
EDIT
I didn't specify what dict was, added it.