View Full Version : initWithContentsOfFile
BollywooD
Apr 25, 2009, 07:44 PM
I am having trouble importing an XML file back into my program, I have it saving OK, but I can't seem to get the strings contained in my plist back into an NSArray (myArray)
here is my code:
- (IBAction)importFile:(id)sender
{
NSOpenPanel *openPanel;
openPanel = [NSOpenPanel openPanel];
[openPanel setCanChooseDirectories:NO];
[openPanel setAllowsMultipleSelection:NO];
[openPanel setResolvesAliases:YES];
[openPanel setCanChooseFiles:YES];
// Only allow the user to select our specific file type.
NSArray *fileTypes = [NSArray arrayWithObjects: @"bundle", nil];
if ([openPanel runModalForTypes:fileTypes] == NSOKButton)
{
NSString *importContentsPath = [[openPanel filename] stringByAppendingPathComponent:@"Contents"];
NSString *importResourcesPath = [importContentsPath stringByAppendingPathComponent:@"Resources"];
//import My strings into myArray
plistPath = [importResourcesPath stringByAppendingPathComponent:@"My.plist"];
NSArray *myArray = [[NSArray alloc] initWithContentsOfFile:[NSString stringWithFormat:@"plistPath"]];
NSLog:(@"contents of myArray %@", myArray);
}
}
when I look at the logs, all I get returned are:
contents of myArray (null)
is there something obvious i am missing?
thanks in advance.
Guiyon
Apr 25, 2009, 07:52 PM
Your call to 'stringWithFormat' is incorrect. What you are actually doing is calling:
NSArray *myArray = [[NSArray alloc] initWithContentsOfFile:@"plistPath"];
I'm assuming that the file "plistPath" probably does not exist. The 'stringWithFormat:' call is also redundant in the code you posted because 'plistPath' is already an NSString*.
BollywooD
Apr 25, 2009, 08:02 PM
Your call to 'stringWithFormat' is incorrect. What you are actually doing is calling:
NSArray *myArray = [[NSArray alloc] initWithContentsOfFile:@"plistPath"];
I'm assuming that the file "plistPath" probably does not exist. The 'stringWithFormat:' call is also redundant in the code you posted because 'plistPath' is already an NSString*.
I fixed that line, and I still get the same result....
the file most definitely does exist, and is in the format:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Files</key>
<array>
<string>Apples</string>
<string>Oranges</string>
<string>Bananas</string>
</array>
</dict>
</plist>
lee1210
Apr 25, 2009, 08:14 PM
Why not toss in an NSLog and print the value of the NSString plistPath, and take a look at what it is? I will admit to being somewhat unfamiliar with NSPanel/NSSavePanel/etc.... but it does strike me as a bit odd that, if this is indeed the case, you want to start your path with the open file. It seems like you'd always keep the plist in a certain place. It also seems like if what is returned is:
/this/is/the/path/to/the/open/file.txt
You'll end up with:
/this/is/the/path/to/the/open/file.txt/Resources/Contents/My.plist
Which doesn't seem like what you'd want.
-Lee
BollywooD
Apr 25, 2009, 08:21 PM
Why not toss in an NSLog and print the value of the NSString plistPath, and take a look at what it is? I will admit to being somewhat unfamiliar with NSPanel/NSSavePanel/etc.... but it does strike me as a bit odd that, if this is indeed the case, you want to start your path with the open file. It seems like you'd always keep the plist in a certain place. It also seems like if what is returned is:
/this/is/the/path/to/the/open/file.txt
You'll end up with:
/this/is/the/path/to/the/open/file.txt/Resources/Contents/My.plist
Which doesn't seem like what you'd want.
-Lee
I tried your suggestion, and the path reurned is correct:
/Users/me/Desktop/My.bundle/Contents/Resources/My.plist
eddietr
Apr 25, 2009, 09:38 PM
The top level container of your plist is a dictionary, not an array.
So you need to use something like +[NSDictionary dictionaryWithContentsOfFile:] or -[NSDictionary initWithContentsOfFile:]
BollywooD
Apr 25, 2009, 10:40 PM
The top level container of your plist is a dictionary, not an array.
So you need to use something like +[NSDictionary dictionaryWithContentsOfFile:] or -[NSDictionary initWithContentsOfFile:]
you are exactly right, many thanks!
I just need to figure out how to convert the NSDictionary to an NSArray of strings....
eddietr
Apr 25, 2009, 10:50 PM
you are exactly right, many thanks!
I just need to figure out how to convert the NSDictionary to an NSArray of strings....
Well, you have to look back at the structure in the plist. It's a dictionary that has a single key/value pair. And the value is an array.
So if you want that array you might do something like this:
NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile: some path];
NSArray *myArray = [myDict objectForKey:@"Files"];
// then save and retain that array however you like
You get the idea, the array you want is inside the dictionary. It's one of the values (in this case the only value).
BollywooD
Apr 25, 2009, 10:53 PM
Well, you have to look back at the structure in the plist. It's a dictionary that has a single key/value pair. And the value is an array.
So if you want that array you might do something like this:
NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile: some path];
NSArray *myArray = [myDict objectForKey:@"Files"];
// then save and retain that array however you like
You get the idea, the array you want is inside the dictionary. It's one of the values (in this case the only value).
thanks, that works perfectly.....
geez, its taking me a while to get my head around Cocoa.
:o
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.