access to plist in app bundle returns null
Asked Answered
Z

3

13

I'm using a piece of code I've used before with success to load a plist into an array:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"species_names" ofType:@"plist"];
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];

The initWithContentsOf File is failing (array is null, suggesting it can't find the plist in the app bundle).

Under the iPhone simulator I've checked that the path created by the first line points to the app file (it does), and used the Finder context menu "Show package contants" to prove to myself that "species_name.plist" is actually in the bundle--with a length that suggests it contains stuff, so it should work (and has in other iOS apps I've written). Suggestions most welcome...

[env Xcode 4.2 beta, iOS 4.3.2].

Zeuxis answered 5/8, 2011 at 5:53 Comment(0)
D
28

You should use initWithContentsOfFile: method of NSDictionary instance, not NSArray. Here is sample:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"species_names" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
NSArray *array = [NSArray arrayWithArray:[dict objectForKey:@"Root"]];

Or this:

NSString *errorDesc = nil;
NSPropertyListFormat format;
NSString    *path = [[NSBundle mainBundle] pathForResource:@"startups" ofType:@"plist"];
NSData      *plistXML = [[NSFileManager defaultManager] contentsAtPath:path];
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization                     
                                          propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                          format:&format
                                          errorDescription:&errorDesc];
NSArray *array = [NSArray arrayWithArray:[temp objectForKey:@"Root"]];


EDIT

You can use initWithContentsOfFile: method of NSArray with file created with writeToFile:atomically: method of NSArray. Plist created with writeToFile:atomically: has this stucture:

<plist version="1.0">
<array>
    ...Content...
</array>
</plist>

And Plist created in XCode has this stucture:

<plist version="1.0">
<dict>
    ...Content...
</dict>
</plist>

Thats why initWithContentsOfFile: method of NSArray dosnt work.

Dennadennard answered 5/8, 2011 at 6:11 Comment(0)
I
3

plist of this type:

enter image description here

OR (basically both are same)

enter image description here

Can be read like this:

NSString *file = [[NSBundle mainBundle] pathForResource:@"appFeatures" ofType:@"plist"];
NSArray *_arrFeats = [NSArray arrayWithContentsOfFile:file];
NSLog(@"%d", _arrFeats.count);

Where appFeatures is name of the plist. (iOS 6.1)

Impersonate answered 12/3, 2013 at 12:55 Comment(0)
U
0

Try this simple metod:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self plistData];
}


- (void)plistData
{
    NSString *file = [[NSBundle mainBundle] pathForResource:@"appFeatures" ofType:@"plist"];
    NSArray *messages = [NSArray arrayWithContentsOfFile:file];
    NSLog(@"%d", messages.count);

    for (int i=0; i<messages.count; i++)
    {
        NSString *data = [messages objectAtIndex:i];
        NSLog(@"\n Data = %@",data);
    }
}
Underfoot answered 31/1, 2014 at 12:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.