tags:

views:

202

answers:

2

Not sure why my .plist looks like this when viwed as source code, and not like an XML file.

{

    authorLastName = Doe;
    authorFirstName = Jane;
    imageFilePath = "NoImage.png";
    title = "Account Test 1";
    year = 2009;
},

{

    authorLastName = Doe;
    authorFirstName = John;
    imageFilePath = "NoImage.png";
    title = "Account Test 2";
    year = 2009;
}

Is the .plist supposed to look like this when viewed as Source Code?

And second, can anyone give an example of how to reutrn the number of items, say, just the last names in each dictionary? I have researched this for too many hours, and Im pulling my hair out cause I cant move on until I get this correct! Every method I try returns 0. Im really new at iPhone programming, so I would appreciate any help I can get! Maybe a full example with reading the plist too?

+1  A: 

A .plist file can take on two forms, either XML or Text. The one that you have is in the text form, and is easier to conceptually understand, while the XML format is... well I'm sure there's some advantage (it's the default when you create a plist. Not sure why yours isn't).

I'm not sure what your question about counting is. Are you asking to find how many items there are (2) or how many of those items have the last name property set (2 in this case) or how many total entries there are (10)?

In any case, you'd start out like this

NSArray *arr = [NSArray arrayWithContentsOfFile:pathToFile];
//Make sure arr is not nil at this point.  If it is, there's either a formatting issue with the file, or the file couldn't be found based on the path you gave

Then, if you were trying for the first case:

int count = arr.count;

Or the second case:

int count = 0;
for (NSDictionary *dict in arr) {
    NSString *lastName = [dict objectForKey:@"authorLastName"];
    if (lastName) {
        ++count;
    }
}

Or the third case:

int count = 0;
for (NSDictionary *dict in arr) {
    count += dict.count;
}
Ed Marty
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *myPlistPath = [documentsDirectory stringByAppendingPathComponent:@"Accounts.plist"]; NSArray *arr = [NSArray arrayWithContentsOfFile:myPlistPath];int count = 0;for (NSDictionary *dict in arr) { count += dict.count;}return count;//////////////////////////The above line ends Simulator with error:Program received signal: “EXC_BAD_ACCESS”.
AWright4911
A: 

I finnaly got the plist count to work!!!

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *myPlistPath = [documentsDirectory 
       stringByAppendingPathComponent:@"Accounts.plist"]; 

NSArray *arr = [NSArray arrayWithContentsOfFile:myPlistPath];

int count = 0;
for (NSDictionary *dict in arr) {
 NSString *lastName = [dict objectForKey:@"authorLastName"];
 if (lastName) {
  ++count;
 }
}
return([NSString stringWithFormat:@"%d Accounts", count]);
AWright4911