MillerMedia
MillerMedia

Reputation: 3671

Passing PList info to a String and then to a Mutable Array (iOS)

I'm having trouble transferring a plist file into a mutable array so I can then use it to populate a table view. I created a plist called MyDeals.plist . I made the root an array and then added two dictionaries as subsets of that array and then populated those dictionaries with strings.

The code is as follows:

NSString *myfile = [[NSBundle mainBundle] pathForResource:@"MyDeals" ofType:@"plist"];
NSLog(@"%@", myfile);
NSMutableArray *myArray = [[NSMutableArray alloc] initWithContentsOfFile:myfile];
NSLog(@"%@", myArray);
self.dealsArray = myArray;
[myArray release];

The first NSLog logs out the correct path for the plist file so it works up until then but then the second log returns '(null)' so myArray doesn't actually grab the info from myfile for some reason. Any ideas? Thanks!

Upvotes: 0

Views: 399

Answers (2)

Cliff
Cliff

Reputation: 11248

I may be wrong but I believe a plist must be deserialized as a dictionary and not an array even if the root element is an array. If you want to desirialize an array from a file it probably should be a file that was created from NSArray writeToFileAtmoically.

Upvotes: 0

rob mayoff
rob mayoff

Reputation: 385680

Try loading the file into an NSData first, so you can see what the problem is.

NSError *error;
NSData *data = [NSData dataWithContentsOfFile:myfile options:0 error:&error];
if (!data) {
    NSLog(@"error loading plist from %@: %@", myfile, error);
    return;
}
self.dealsArray = [NSPropertyListSerialization propertyListWithData:data
    options:NSPropertyListMutableContainers format:NULL error:&error];
if (!self.dealsArray) {
    NSLog(@"error parsing plist from %@: %@", myfile, error);
    return;
}

Upvotes: 1

Related Questions