Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Mac OS X Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-14-2011, 12:18 PM   #1 (permalink)
Token farm animal
 
sneaky's Avatar
 
Join Date: Apr 2011
Posts: 321
sneaky is on a distinguished road
Default [[NSBundle mainBundle] bundlePath] equivalent on OSX?

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:
Code:
	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"?
sneaky is offline   Reply With Quote
Old 11-29-2011, 04:06 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,005
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by sneaky View Post
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:
Code:
	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

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-12-2012, 06:56 AM   #3 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 5
ddeaco is on a distinguished road
Default

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 -
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
ddeaco is offline   Reply With Quote
Old 01-12-2012, 08:54 AM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,005
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by ddeaco View Post
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 -
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 pathForResourcefType: 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

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 369
10 members and 359 guests
Creativ, Emy, eski, husthlj, illogical, LegionMD, LunarMoon, padsoftware, stanny, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,677
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, husthlj
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:17 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0