kid_x
kid_x

Reputation: 1475

-[NSDictionary dictionaryWithContentsOfFile] returning nil on iPad; returning dictionary in the simulator

I have a method:

#define ROOT_ASSETS_PATH @"/assets/"

...

-(NSMutableDictionary*) getContentsOfPlist{
    NSString *pageContentPlistPath = [[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent: ROOT_ASSETS_PATH] stringByAppendingPathComponent:@"content.plist"];

    NSMutableDictionary *mDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:pageContentPlistPath];
    return mDicitonary;
}

On the simulator. this returns a dictionary. On the iPad, it returns nil. Made sure the extension case matches that in the call as well.

Any ideas?

Upvotes: 0

Views: 1621

Answers (4)

kid_x
kid_x

Reputation: 1475

The issue was case after all. Case of the filename didn't match the case I put in the parameter. OSX doesn't care, but the iPad does.

Upvotes: 2

Nikos M.
Nikos M.

Reputation: 13783

+ (id)dictionaryWithContentsOfFile:(NSString *)path

Parameters

path

A full or relative pathname. The file identified by path must contain a string representation of a property list whose root object is a dictionary.

Return Value

A new dictionary that contains the dictionary at path, or nil if there is a file error or if the contents of the file are an invalid representation of a dictionary.

So, you have to set a full path to your file (or an appropriate relative path)

Note also that the app bundle is read-write on Simulator but read-only on device.

Upvotes: 0

Vytautas
Vytautas

Reputation: 573

Also it is good idea to use:

[NSPropertyListSerialization propertyListWithData:options:format:error:]

Instead of direct NSDictionary initialization. If something will fail, it should return a nice error for you.

Upvotes: 0

Steve
Steve

Reputation: 1840

Try removing the slashes in your #define. stringByAppendingPathComponent will add the slashes for you. Not sure why this would work in the Simulator.

Upvotes: 0

Related Questions