Re: adding an additional plist files as resources? possible?
Re: adding an additional plist files as resources? possible?
- Subject: Re: adding an additional plist files as resources? possible?
- From: "b.bum" <email@hidden>
- Date: Tue, 28 Sep 2004 09:46:30 -0700
On Sep 27, 2004, at 6:21 PM, Ken Hawkins wrote:
everytime i send a question i answer it myself. sorry for taking space up here and you folks having to be a rhetorical sounding board.
i just copied the plist into the resources dir at build time and then
infoDictionary = [NSDictionary dictionaryWithContentsOfFile:@"subjects.plist"];
// If we succeeded, look for our property.
if ( infoDictionary != NULL ) {
subjects = [infoDictionary objectForKey:@"BJSubjectTypes"];
NSLog([subjects description]);
}
That is unlikely to work over time. In particular, your call to + dictionaryWithContentsOfFile: assumes that @"subjects.plist" is in the current working directory. That may be true during development, but it certainly won't be true at runtime.
The NSBundle class is provided to address these kinds of problems.
First, add subjects.plist to the resources of your project (no need to do a copy files phase or anything else-- it'll "just work" given the default build phases).
Next, you'll want code like this:
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *subjectsPlistPath = [mainBundle pathForResource: @"subjects" ofType: @"plist"];
NSDictionary *subjectsPlist = [NSDictionary dictionaryWithContentsOfFile: subjectsPlistPath];
NSDictionary *subjects = [subjectsPlist objectForKey: @"BJSUbjectTypes"];
Typed in mail... bound to not compile after copy/paste (but should be close). All of this could be written as one dense line of code, if desired, but I lean to the above because you can set a breakpoint at each step, if needed.
b.bum
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden