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

BollywooD

macrumors 6502
Original poster
Apr 27, 2005
374
47
Hamburg
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:

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:
Code:
contents of myArray (null)

is there something obvious i am missing?

thanks in advance.
 
Your call to 'stringWithFormat' is incorrect. What you are actually doing is calling:
Code:
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*.
 
Your call to 'stringWithFormat' is incorrect. What you are actually doing is calling:
Code:
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:
Code:
<?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>
 
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
 
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:
Code:
/Users/me/Desktop/My.bundle/Contents/Resources/My.plist
 
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:]
 
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....
 
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:

Code:
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).
 
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:

Code:
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.
:eek:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.