Re: XML plist to NSArray
Re: XML plist to NSArray
- Subject: Re: XML plist to NSArray
- From: Michael Hanna <email@hidden>
- Date: Sat, 7 Jun 2003 16:28:03 -0400
hi this is helpful, but I'm stuck in a spot. I get a sigbus 10 error
when I try to load the bundlePath. I put all this in the main.m
file..Since it's a fairly simple program I didn't create a class to
load the plist data.
---
code: main.m
---
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSMutableArray * lines;
NSBundle *mainBundle;
NSString *plistPath;
NSString *bundPath;
NSString *nameWithoutSuffix;
NSDictionary *allPhrasesDict;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
nameWithoutSuffix = @"Phrases";
NSLog ( @"Made namewithoutsuffix");
bundPath = [mainBundle bundlePath];
NSLog ( @"got bundlepath");
plistPath = [NSString
stringWithFormat:@"%@/Contents/Resources/%@.plist", bundPath,
nameWithoutSuffix];
NSLog ( @"generated plist path");
allPhrasesDict = [[NSMutableDictionary alloc]
initWithContentsOfFile:plistPath];
NSLog ( @"Phrases are made of this\n%@", [allPhrasesDict
description] );
[pool release];
return 0;
} //main.m
---
output:
---
2003-06-07 16:21:07.692 countrygen[5175] Made namewithoutsuffix
countrygen has exited due to signal 10 (SIGBUS).
---
so it seems to get stuck at this line:
bundPath = [mainBundle bundlePath];
I'm also unsure if this line will display the dictionary with arrays as
values:
NSLog ( @"Phrases are made of this\n%@", [allPhrasesDict
description] );
any ideas?
Michael
On Saturday, June 7, 2003, at 01:18 PM, Danny Swarzman wrote:
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.
_______________________________________________
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.