4

I am working on building a PLIST that will essentially be a directory. I would like to parse the plist so I may display the last names in a Table View, and when selected, populate the next view's labels with things such as addresses, phone numbers, and emails. I could use some guidance, here is what I have created so far:

<?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> <array> <string>Last Name</string> <string>First Name</string> <string>Address</string> <string>Phone Number</string> <string>Email</string> </array> <array> <string>Last Name</string> <string>First Name</string> <string>Address</string> <string>Phone Number</string> <string>Email</string> </array> <array> <string>Last Name</string> <string>First Name</string> <string>Address</string> <string>Phone Number</string> <string>Email</string> </array>

Is this plist setup well, or do I need to make changes. If setup well, I'm just a little lost on parsing. I have used GDATAXML in past with podcasts, but unsure how it would relate with the PLIST in-app.

user717452
  • 33
  • 14
  • 73
  • 149

4 Answers4

5

If you want to load a plist into an array or dictionary, just use the following methods:

NSArray * myArray = [NSArray arrayWithContentsOfFile: <plist-file-path>];

NSDictionary * myDictionary = [NSDictionary dictionaryWithContentsOfFile: <plist-file-path>];

To create a plist in Xcode, just choose File->New and select the Property List template from the Resource section. You'll then be presented with the property list editor, so no need to try and code by hand.

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
1

Do not try parsing the xml from your plist.

You have to pass the plist to NSDictionary instead:

NSString *pathToPlist = [NSString stringWithFormat:@"%@/your_plist_file.plist",[[NSBundle mainBundle]resourcePath]];
NSDictionary *plistDictionary = [NSDictionary dictionaryWithContentsOfFile:pathToPlist];

//now you can access the keys and values:
for (NSString *key in plistDictionary) 
    NSLog(@"key = %@ and value = %@", key, [plistDictionary objectForKey:key]);

//or pass all values to an array:
NSArray *valArray = [plistDictionary allValues];
John Smith
  • 2,012
  • 1
  • 21
  • 33
1

In Swift 3:

if let path = Bundle.main.path(forResource: "FileName", ofType: "plist"), 
   let dict = NSDictionary(contentsOfFile: path) as? [String: AnyObject] {
    // use swift dictionary as normal
}

If your first node is an array, you can use arrays like this (in this case, the array contains dictionary elements):

if let path = Bundle.main.path(forResource: "FileName", ofType: "plist"),
   let array = NSArray(contentsOfFile: path) as? [Dictionary<String, Any>] {
    // use swift array as normal
}
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
Paolo Musolino
  • 614
  • 7
  • 9
-4

Use NSXMLParser. Documentation is here.

Basically you will obtain a NSDictionary with contents of file. Then process it using

[aDictionary objectForKey:@"foo"];
Ankur
  • 5,086
  • 19
  • 37
  • 62
wczekalski
  • 720
  • 4
  • 21
  • Worst answer ever on SO! Why use NSXML parser when there's a built in method to do just this? – Ashley Mills Aug 17 '12 at 08:42
  • No, not the worst.Built in methods are not always the best. Think about nsstringwithcontentsofURL. It's terribly slow, and not flexible. NSXMLParser or some third party library will be faster than contentsOfFile. – wczekalski Aug 17 '12 at 08:50
  • You're just wrong. Using `stringWithContentsOfFile:` is optimised by Apple to work with plists. – Ashley Mills Aug 20 '12 at 10:11