An Array Of Dictionaries Not Writing To File Correctly?

Discussion in 'iOS Programming' started by Darkroom, Aug 29, 2009.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    not sure what is my error. i'm attempting to create a .plist that is an array of dictionaries. only the root (array) of the .plist is being created, while the dictionary object is missing.

    Code:
    - (NSString *)measurementsDataPath
    	{
    	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    	NSString *documentsDirectory = [paths objectAtIndex:0];
    	return [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
    	}
    
    - (void)viewWillAppear:(BOOL)animated
    	{
    	NSMutableDictionary *dictionaryItem = [[NSMutableDictionary alloc] init];
    	[dictionaryItem setObject:[NSString stringWithFormat:@"Name String Test"] forKey:@"name"];
    	[dictionaryItem setObject:[NSString stringWithFormat:@"Measurements String Test"] forKey:@"measurement"];
    	[self.measurementsDataArray addObject:dictionaryItem];
    	[self.measurementsDataArray writeToFile:[self measurementsDataPath] atomically:YES];
    ...
    
    self.measurementsDataArray is an NSMutableArray object.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    If you NSLog() your array before you write it to file, does it show everything correctly?
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    it does not.

    Code:
    	NSMutableDictionary *dictionaryItem = [[NSMutableDictionary alloc] init];
    	[dictionaryItem setObject:[NSString stringWithFormat:@"Name String Test"] forKey:@"name"];
    	[dictionaryItem setObject:[NSString stringWithFormat:@"Measurements String Test"] forKey:@"measurement"];
    	[measurementsDataArray addObject:dictionaryItem];
    	NSLog(@"%@", measurementsDataArray);
    
    NSLog returns (null)
    :confused:
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Can you post the code that initializes the array?
     
  5. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #5
    the array is declared in the header and synthesized. is this error happening because of that?
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    When you synthesize an object you're only creating the accessor methods to access that object. You still need to initialize the object like any other.
     
  7. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #7
    ah of course. silly me. the following works:

    Code:
    	NSMutableDictionary *dictionaryItem = [[NSMutableDictionary alloc] init];
    	[dictionaryItem setObject:[NSString stringWithFormat:@"Name String Test"] forKey:@"name"];
    	[dictionaryItem setObject:[NSString stringWithFormat:@"Measurements String Test"] forKey:@"measurement"];
    	measurementsDataArray = [[NSMutableArray alloc] init];
    	[measurementsDataArray addObject:dictionaryItem];
    	[dictionaryItem release];
    	[measurementsDataArray writeToFile:[self measurementsDataPath] atomically:YES];
    
     

Share This Page