Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

JohnWi

macrumors newbie
Original poster
Nov 11, 2009
9
0
Hello everyone,

I am trying to create a NSString called filePath that I want to use to write entries into a plist. I use

filePath = [[[NSBundle mainBundle] pathForResource:mad:"test" ofType:mad:"plist"] retain];

to define the path, but if I log filePath, it returns nil. Does anyone know what is not working here?

Thx, John
 
Hello everyone,

I am trying to create a NSString called filePath that I want to use to write entries into a plist. I use

filePath = [[[NSBundle mainBundle] pathForResource:mad:"test" ofType:mad:"plist"] retain];

to define the path, but if I log filePath, it returns nil. Does anyone know what is not working here?

Thx, John

Use this:

Code:
NSBundle *bundle = [NSBundle mainBundle];
NSString *filePath = [bundle pathForResource:@"test" ofType:@"plist"];
 
That would give you the path of a plist within your application (if it exists). I'm quite sure you don't want to modify a plist within your application. Even if you _think_ you want to modify it, you still don't want to do it.
 
Per the documentation, the method returns nil if the resource could not be located. The method is for checking for a resource before loading. I'm guessing you're wanting to create a new file rather than loading an existing one. If that is the case, try this instead:

Code:
NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"test.plist"];
 
That would give you the path of a plist within your application (if it exists). I'm quite sure you don't want to modify a plist within your application. Even if you _think_ you want to modify it, you still don't want to do it.

In particular, this has a tendency to break code signing.
 
Hello everyone,

thx a lot for the help. I used the solution posted by lucasgladding and everything works the way I want it to.
 
Hello everyone,

thx a lot for the help. I used the solution posted by lucasgladding and everything works the way I want it to.

What happens when you copy your application onto a CD or a read-only disk image? What happens when two users on the same computer use your application?
 
What happens when you copy your application onto a CD or a read-only disk image? What happens when two users on the same computer use your application?

For the record, I do agree with everyone else on this thread about storing within the application bundle. See the "Low Level File Management Programming Guide" and "Creating Paths and Locating Directories" for information about alternative system paths. Your application support directory on the Mac or the documents folder on the iPhone make the most sense typically. If you generate Core Data projects for either platform, you will find example code in the application controller.

Best of luck

Luke
 
What happens when you copy your application onto a CD or a read-only disk image? What happens when two users on the same computer use your application?

Hm, I see what you mean. But to be honest, the application I write is mostly a prove-of-concept thing so I do not worry about the obstacles you mention (at least not yet). I guess I will have to come up with a better solution if the project grow. Thx again.
 
Hm, I see what you mean. But to be honest, the application I write is mostly a prove-of-concept thing so I do not worry about the obstacles you mention (at least not yet). I guess I will have to come up with a better solution if the project grow. Thx again.

Simple: Preference data goes into the preference folder, not in the application bundle. The preference folder is always writeable, and there is always one preference folder per user. And most likely you don't care about the location of the preference folder at all, you just add or write preferences and Cocoa will put them into the right place.

So the problem that you are going to run into is not one that you will need to solve, the whole approach is wrong.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.