Re: XML plist to NSArray
Re: XML plist to NSArray
- Subject: Re: XML plist to NSArray
- From: Danny Swarzman <email@hidden>
- Date: Sat, 7 Jun 2003 10:18:11 -0700
Here's a method that works for me:
- (NSMutableDictionary*)plistFromBundle:(NSString*)nameWithoutSuffix
//
// Get the dictionary for the plist in the application bundle.
//
{
NSString* bundlePath = [mBundle bundlePath];
NSString* plistPath = [NSString stringWithFormat:
@"%@/Contents/Resources/%@.plist"</colo
r>, bundlePath, nameWithoutSuffix];
NSMutableDictionary* plistDictionary = [[NSMutableDictionary alloc]
initWithContentsOfFile:plistPath];
return plistDictionary;
}
If your code has something like:
NSLog ( @"Pharses are made of this\n%@", [[myClass plistFromBundle
</fontfami
ly>:"Phrases"] description] );
you should see it.
I don't where Apple explains that you need "Contents/Resources" in your
path. I discovered it by logging directory lists until I found it.
-Danny
At 9:20 -0700 6/7/03, Michael Hanna wrote:
>
PB v.2.1, OS X 10.2.6
>
>
Hi, I'm making a Foundation tool based on this code I found on the net.
>
>
I've stored snippets of phrases in an XML plist I made in Property List
>
Editor.
>
>
I added the "Phrases.plist" to my project in PB. A dictionary with keys
>
matched to arrays of phrases.
>
>
Then I tried accessing the plist, but I get (null) back...Is my code
>
not properly assigning the NSStrings to the arrays?
>
>
here's a portion of my plist, then code...
>
>
---
>
>
<?xml version="1.0" encoding="UTF-8"?>
>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
>
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
>
<plist version="1.0">
>
<dict>
>
<key>phrase_01</key>
>
<array>
>
<string>on the highway</string>
>
<string>in Sheboygan</string>
>
<string>outside Fresno</string>
>
<string>at a truck stop</string>
>
<string>on probation</string>
>
<string>in a jail cell</string>
>
<string>in a nightmare</string>
>
<string>incognito</string>
>
<string>in the Stone Age</string>
>
<string>in a treehouse</string>
>
<string>in a gay bar</string>
>
</array>
>
<key>phrase_02</key>
>
<array>
>
<string>in September</string>
>
.... and so on
>
>
---
>
code - main.m
>
---
>
>
#import <Foundation/Foundation.h>
>
>
int main (int argc, const char * argv[]) {
>
>
NSMutableArray * lines;
>
>
NSArray * phrases01;
>
NSArray * phrases02;
>
NSArray * phrases03;
>
NSArray * phrases04;
>
NSArray * phrases05;
>
NSArray * phrases06;
>
NSArray * phrases07;
>
NSArray * phrases08;
>
NSArray * phrases09;
>
NSArray * phrases10;
>
NSArray * phrases11;
>
NSArray * phrases12;
>
>
NSString *path;
>
NSDictionary *allPhrases;
>
>
>
>
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
>
>
path = [[NSBundle mainBundle ] pathForResource:@"Phrases"
>
ofType:@"plist"];
>
allPhrases = [NSDictionary dictionaryWithContentsOfFile: path];
>
>
phrases01 = [allPhrases objectForKey:@"phrases_01"];
>
phrases02 = [allPhrases objectForKey:@"phrases_02"];
>
phrases03 = [allPhrases objectForKey:@"phrases_03"];
>
phrases04 = [allPhrases objectForKey:@"phrases_04"];
>
phrases05 = [allPhrases objectForKey:@"phrases_05"];
>
phrases06 = [allPhrases objectForKey:@"phrases_06"];
>
phrases07 = [allPhrases objectForKey:@"phrases_07"];
>
phrases08 = [allPhrases objectForKey:@"phrases_08"];
>
phrases09 = [allPhrases objectForKey:@"phrases_09"];
>
phrases10 = [allPhrases objectForKey:@"phrases_10"];
>
phrases11 = [allPhrases objectForKey:@"phrases_11"];
>
phrases12 = [allPhrases objectForKey:@"phrases_12"];
>
>
NSLog(@"phrases 1 = %@", phrases01);
>
>
// insert code here...
>
//NSLog(@"Hello, World!");
>
[pool release];
>
return 0;
>
} // main
>
>
the NSLog statement results in:
>
>
2003-06-07 12:08:38.700 countrygen[4094] phrases 1 = (null)
>
>
countrygen has exited with status 0.
>
>
>
any help would be appreciated..
>
Michael
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.