Reading from plist

Discussion in 'iPhone/iPad Programming' started by jschulbach, Jul 22, 2010.

  1. macrumors newbie

    Joined:
    Jun 29, 2010
    #1
    I'm attempting to load data from a plist file that I've placed in my resources folder in xcode, it's named "FakeData.plist". When I try loading it into a mutable dictionary via the following code the resulting dictionary is nil. I searched the forum and online and have tried a couple different approaches I have seen but am obviously missing something.

    Code:
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    	NSString *documentsPath = [paths objectAtIndex:0];
    	
    	NSString *filePath = [documentsPath stringByAppendingPathComponent:@"FakeData.plist"];
    	NSMutableDictionary * plistArr = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    	if(plistArr){
    		NSLog(@"value is: %@", [plistArr count]);
    	}
    
    
    If I log filePath it shows the full file path so it's getting that correctly, but if I log plistArr I get nil, if I log [plistArr count] it returns 0. Could it be rejecting the plist file? I've logged everything and can't figure out the next step in debugging or why it isn't loading the plist into the dictionary.
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    So, you've placed the plist in your Resources but are trying to load it from your Documents directory?
     
  3. thread starter macrumors newbie

    Joined:
    Jun 29, 2010
    #3
    I want to read it from the resources although it does seem to find it in the file in the path search? How do I get the path to the files in resources?
     
  4. macrumors member

    ritsard

    Joined:
    Jun 18, 2009
    Location:
    SF Bay Area, CA
    #4
    you may want to look at NSBundle
     
  5. thread starter macrumors newbie

    Joined:
    Jun 29, 2010
    #5
    Thanks ritsard, that was what I needed. Also for future reference for anyone else with a similar issue, I was trying to load the plist into a dictionary when my plist was set up as an array, switched to an array and it loaded in fine. Here is the final code snippet that worked for me.

    Code:
    NSString   *path;
    	
    	NSBundle *thisBundle = [NSBundle bundleForClass:[self class]];
    	if (path = [thisBundle pathForResource:@"FakeData" ofType:@"plist"]) {
    		NSLog(@"value is: %@", path);
    		NSMutableArray * plistArr = [[NSMutableArray alloc] initWithContentsOfFile:path];
    		NSLog(@"value is: %@", plistArr);
    	}
    
     

Share This Page