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];
Related
I tried to create .plist at /Library/LaunchDaemons but it doesn't run.
It follows below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.verdaccio</string>
<key>Program</key>
<string>/usr/local/bin/verdaccio</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
Thanks in advance.
The right answer to have it working is as below:
<?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>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
</dict>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>org.verdaccio</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/verdaccio</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>igor</string>
<key>SessionCreate</key>
<true/>
</dict>
</plist>
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.
First, a little background. I am trying to make a platform game. In front of the game layer I want "lights" to go by and by lights I mean white triangles. I am trying to get coordinates for the triangles from a plist.
At this point I have spent days trying to get my app to read from a plist and I cannot get it to work.
This is what I currently have and this is basically what the internet is saying I should have and that it should work.
In helloWorld.h:
NSMutableArray *foregroundLights;
In helloWorld.m
NSString *path = [[NSBundle mainBundle] pathForResource:#"foregroundlights" ofType:#"plist"];
foregroundLights = [[NSMutableArray alloc] initWithContentsOfFile:path];
I have tried formatting the patch every way that I can think of, I have tried using NSSearchPathsForDirectoriesInDomains. I have tried making the plist a dictionary and making it an array. What am I missing?
Here is a piece of the plist. The full one contains 50 arrays of 4 ints.
<?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>root</key>
<array>
<array>
<integer>128</integer>
<integer>68</integer>
<integer>246</integer>
<integer>7</integer>
</array>
<array>
<integer>181</integer>
<integer>84</integer>
<integer>170</integer>
<integer>62</integer>
</array>
<array>
<integer>118</integer>
<integer>51</integer>
<integer>142</integer>
<integer>64</integer>
</array>
</array>
</dict>
</plist>
I am using the route me frame work for my map application.
I badly want to cache more tiles than its caching by default.
So i changed the routeme.plist like this:
<?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">
<array>
<dict>
<key>capacity</key>
<integer>16</integer>
<key>type</key>
<string>db-cache</string>
</dict>
<dict>
<key>capacity</key>
<integer>100000</integer>
<key>minimalPurge</key>
<integer>5000</integer>
<key>strategy</key>
<string>LRU</string>
<key>type</key>
<string>memory-cache</string>
<key>useCachesDirectory</key>
<true/>
</dict>
</array>
</plist>
But it doesn't matter which values i change, the app always reloads tiles which i have already seen before. Is there a limit for caching?
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.