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];
}
Related
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);
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"]];
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.
I am just starting to work with preference files and I started having problems immediately when editing the root.plist in my settings bundle. My XCode 4 crashes every time I add a property to the plist while editing it as a property list. So I thought I would simply edit as source code. It seems to be a lot easier.
But when I run the program, the root.plist is not being read. (It was working fine with a settings bundle from a demo program. I'm using InAppSettingsKit.) I looked at the root.plist in the source code editor and it looks right. I tried to look at it as a property list and I get an error that says the plist is corrupted.
Here is the contents of my plist. Can someone tell me what is wrong with it?
<?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>PreferenceSpecifiers</key>
<array>
<!-- Databases -->
<dict>
<key>Title</key> <string>Databases</string>
<key>Type</key> <string>PSGroupSpecifier</string>
</dict>
<dict>
<key>Type</key> <string>PSToggleSwitchSpecifier</string>
<key>Title</key> <string>db0</string>
<key>Key</key> <string>db0_preference</string>
<key>DefaultValue</key> <true/>
</dict>
<dict>
<key>Type</key> <string>PSToggleSwitchSpecifier</string>
<key>Title</key> <string>db1</string>
<key>Key</key> <string>db1_preference</string>
<key>DefaultValue</key> <true/>
</dict>
<!-- Sharing Actions -->
<dict>
<key>Type</key> <string>PSGroupSpecifier</string>
<key>Title</key> <string>Sharing Actions</string>
</dict>
<dict>
<key>Type</key> <string>PSToggleSwitchSpecifier</string>
<key>Title</key> <string>Facebook</string>
<key>Key</key> <string>facebook_preference</string>
<key>DefaultValue</key> <true/>
</dict>
<dict>
<key>Type</key> <string>PSToggleSwitchSpecifier</string>
<key>Title</key> <string>Twitter</string>
<key>Key</key> <string>twitter_preference</string>
<key>DefaultValue</key> <true/>
</dict>
<dict>
<key>Type</key> <string>PSToggleSwitchSpecifier</string>
<key>Title</key> <string>Email</string>
<key>Key</key> <string>email_preference</string>
<key>DefaultValue</key> <true/>
</dict>
<dict>
<key>Type</key> <string>PSToggleSwitchSpecifier</string>
<key>Title</key> <string>BlogSpot</string>
<key>Key</key> <string>blogspot_preference</string>
<key>DefaultValue</key> <true/>
</dict>
<!-- Automatic Email Enable -->
<dict>
<key>Type</key> <string>PSGroupSpecifier</string>
<key>Title</key> <string>Automatic Emailing</string>
</dict>
<dict>
<key>Type</key> <string>PSToggleSwitchSpecifier</string>
<key>Title</key> <string>Always Send to Email</string>
<key>Key</key> <string>autoblogspot_preference</string>
<key>DefaultValue</key> <true/>
</dict>
<!-- Calendar -->
<dict>
<key>Type</key> <string>PSRadioGroupSpecifier</string>
<key>Title</key> <string>First Day of the Week</string>
<key>Key</key> <string>firstDayOfTheWeek_preference</string>
<key>Values</key>
<array>
<integer>0</integer>
<integer>1</integer>
<integer>2</integer>
<integer>3</integer>
<integer>4</integer>
<integer>5</integer>
<integer>6</integer>
</array>
<key>Titles</key>
<array>
<string>Sunday</string>
<string>Monday</string>
<string>Tuesday</string>
<string>Wednesday</string>
<string>Thursday</string>
<string>Friday</string>
<string>Saturday</string>
</array>
</dict>
<dict>
<key>
</array>
<key>StringsTable</key>
<string>Root</string>
</dict>
</plist>
At the very end of the plist you have a dangling key and dict tag.
Lines #90 and #91.
</dict>
<dict>
<key>
</array>
<key>StringsTable</key>
<string>Root</string>
</dict>
</plist>
Should be something like:
</dict>
<dict />
</array>
<key>StringsTable</key>
<string>Root</string>
</dict>
</plist>
or
</dict>
<dict>
<key>key</key><string>string</string>
</dict>
</array>
<key>StringsTable</key>
<string>Root</string>
</dict>
</plist>
I found this out using TextMate. Bundles -> Property List -> Validate Syntax. Doesn't tell you the exact problem, but gets you to the area.
You can also get a line # to look at by trying to open the plist in the Property List Editor app (/Developer/Applications/Utilities/Property List Editor.app)
Plists are XML, so any XML validator will find major problems in your syntax. Rule of thumb, though, is for every tag you need a close tag. For every key you need a value.
Empty tags should be <tag /> not <tag></tag>.
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.