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

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
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.
 
If you NSLog() your array before you write it to file, does it show everything correctly?
 
If you NSLog() your array before you write it to file, does it show everything correctly?

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