Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

[[NSBundle mainBundle] bundlePath] equivalent on OSX?

sneakysneaky Posts: 349Registered Users
I started developing for iOS without any previous experience on Mac which seems to come back and bite me.

I have got a small utility app I use to preload data into a sqlite file so I don't have to do this in my iphone app on first launch.

I'm trying to load a plist the same way I'd do on the iphone but that doesn't seem to work:

NSArray *arr = nil;
NSString *arrPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@\"myList.plist\"];
if (arrPath) {
arr = [NSArray arrayWithContentsOfFile:arrPath];
if (arr) {
NSLog(@\"Count %lu\", [arr count]);
}
else {
NSLog(@\"nil\");
}
}


I get 'nil' printed in the console. How can I easily load a plist that is "in my bundle"?
Post edited by sneaky on

Replies

  • Duncan CDuncan C Posts: 8,015Tutorial Authors, Registered Users
    sneaky;388695 said:
    I started developing for iOS without any previous experience on Mac which seems to come back and bite me.

    I have got a small utility app I use to preload data into a sqlite file so I don't have to do this in my iphone app on first launch.

    I'm trying to load a plist the same way I'd do on the iphone but that doesn't seem to work:

    NSArray *arr = nil;
    NSString *arrPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@\"myList.plist\"];
    if (arrPath) {
    arr = [NSArray arrayWithContentsOfFile:arrPath];
    if (arr) {
    NSLog(@\"Count %lu\", [arr count]);
    }
    else {
    NSLog(@\"nil\");
    }
    }


    I get 'nil' printed in the console. How can I easily load a plist that is "in my bundle"?
    I developed for Mac for years before iOS came along.

    That code should work.

    Break it down and use a separate variable for each step.
    • Get the main bundle.
    • Use that to get the bundle path.
    • Then use THAT to build your full path to your file.
    Step through the code in the debugger and check the result of each step.

    My guess is that it's the call to arrayWithContentsOfFile: that's failing. That will fail if the file you're trying to read is not a well formed plist file. THAT will happen if any of the items in the array are not "property list" objects. That is the exact same thing that will happen in iOS.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • ddeacoddeaco Posts: 5New Users
    Hi,

    I think im having the same problem, did you manage to fix the problem?

    I'm porting my game to the mac, it is going ok but I have hit a problem I can't seem to fix.

    In the game I save all the levels into pList from our external editor and then load them into the game using the following code, which works great on iOs, but on OSX it just doesn't find the files.

    iOS code -

    NSString* levelString = [NSString stringWithFormat:@\"L%d.plist\", level];
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *finalPath = [path stringByAppendingPathComponent:levelString];
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:levelString];

    NSLog(@\"Final Path = %@\",finalPath);

    //Do Load Level...


    The output from that NSLog reads as follows -

    Final Path = /Users/daviddeacon/Library/Developer/Xcode/DerivedData/GameMac-bphkstcwuqklgxbgdhezeutagblu/Build/Products/Release/GameMac.app/L1101.plist

    I started developing on iOS first so haven't got a base knowledge of the differences between the to platforms, Is the mainBundle path much different?

    Thanks, David
  • Duncan CDuncan C Posts: 8,015Tutorial Authors, Registered Users
    ddeaco;401225 said:
    Hi,

    I think im having the same problem, did you manage to fix the problem?

    I'm porting my game to the mac, it is going ok but I have hit a problem I can't seem to fix.

    In the game I save all the levels into pList from our external editor and then load them into the game using the following code, which works great on iOs, but on OSX it just doesn't find the files.

    iOS code -

    NSString* levelString = [NSString stringWithFormat:@\"L%d.plist\", level];
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *finalPath = [path stringByAppendingPathComponent:levelString];
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:levelString];

    NSLog(@\"Final Path = %@\",finalPath);

    //Do Load Level...


    The output from that NSLog reads as follows -

    Final Path = /Users/daviddeacon/Library/Developer/Xcode/DerivedData/GameMac-bphkstcwuqklgxbgdhezeutagblu/Build/Products/Release/GameMac.app/L1101.plist

    I started developing on iOS first so haven't got a base knowledge of the differences between the to platforms, Is the mainBundle path much different?

    Thanks, David
    The path will have different subdirectories in it, yes. As long as you are not getting nil, you should be fine.


    You might want to use the NSBundle method pathForResource:ofType: instead of constructing a path yourself. That takes care of navigating into the bundle and handing you back a path to the file you ask for.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
Sign In or Register to comment.