initWithContentsOfFile

Discussion in 'Mac Programming' started by BollywooD, Apr 25, 2009.

  1. BollywooD macrumors 6502

    BollywooD

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #1
    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.
     
  2. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #2
    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*.
     
  3. BollywooD thread starter macrumors 6502

    BollywooD

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #3
    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>
    
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    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
     
  5. BollywooD thread starter macrumors 6502

    BollywooD

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #5
    I tried your suggestion, and the path reurned is correct:
    Code:
    /Users/me/Desktop/My.bundle/Contents/Resources/My.plist
     
  6. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #6
    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:]
     
  7. BollywooD thread starter macrumors 6502

    BollywooD

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #7
    you are exactly right, many thanks!

    I just need to figure out how to convert the NSDictionary to an NSArray of strings....
     
  8. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #8
    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).
     
  9. BollywooD thread starter macrumors 6502

    BollywooD

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #9
    thanks, that works perfectly.....

    geez, its taking me a while to get my head around Cocoa.
    :eek:
     

Share This Page