I have 4 NSMutableArray, and on launch they are properly initialized. In a section of my code, I write the contents of these arrays to individual plist files, and these plist files have been created and added to the project. I have 4 instances of writing to file, and for some reason only the first 2 write to file successfully (didWrite and didWrite2) but didWrite3 and didWrite 4 return “nope” all the time meaning an unsuccessful write. Here is the code that initializes and allocates the arrays in applicationDidFinishLaunching:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *DataPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Data.plist"];
NSString *inkDatesPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"inkDates.plist"];
NSString *inkFontPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"inkFont.plist"];
NSString *inkColorPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"inkColor.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: DataPath])
{
inks=[[NSMutableArray alloc] init ];
NSLog(@"initialized inks as new");
}
else inks=[[NSMutableArray alloc] initWithContentsOfFile:DataPath];
if (![fileManager fileExistsAtPath: inkDatesPath])
{
inkDates=[[NSMutableArray alloc] init ];
}
else inkDates=[[NSMutableArray alloc] initWithContentsOfFile:inkDatesPath];
if (![fileManager fileExistsAtPath: inkFontPath])
{
inkFont=[[NSMutableArray alloc] init ];
}
else inkFont=[[NSMutableArray alloc] initWithContentsOfFile:inkFontPath];
if (![fileManager fileExistsAtPath: inkColorPath])
{
inkColor=[[NSMutableArray alloc] init ];
NSLog(@"initialized inkColor as new");
}
else {
inkColor=[[NSMutableArray alloc] initWithContentsOfFile:inkColorPath];
NSLog(@"initialized inkColor as copy");
When the app is first launched, I get “initialized inkColor as new”, but I still get a fail to write. inks and inkDates write properly but the other two dont. And here is my code that tries to write to the plist files:
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
NSString *documentsDirectory = [paths objectAtIndex:0]; //2
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Data.plist"]; //3
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle=[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
}
NSString *pathDates = [documentsDirectory stringByAppendingPathComponent:@"inkDates.plist"]; //3
NSFileManager *fileManagerDates = [NSFileManager defaultManager];
if (![fileManagerDates fileExistsAtPath: pathDates])
{
NSString *bundle=[[NSBundle mainBundle] pathForResource:@"inkDates" ofType:@"plist"];
[fileManagerDates copyItemAtPath:bundle toPath: pathDates error:&error]; //6
}
NSFileManager *fileManagerFont = [NSFileManager defaultManager];
NSString *pathFont = [documentsDirectory stringByAppendingPathComponent:@"inkFont.plist"];
if (![fileManagerFont fileExistsAtPath: pathFont])
{
NSString *bundle=[[NSBundle mainBundle] pathForResource:@"inkFont" ofType:@"plist"];
[fileManagerFont copyItemAtPath:bundle toPath: pathFont error:&error]; //6
}
NSFileManager *fileManagerColor = [NSFileManager defaultManager];
NSString *pathColor = [documentsDirectory stringByAppendingPathComponent:@"inkColor.plist"];
if (![fileManagerFont fileExistsAtPath: pathColor])
{
NSString *bundle=[[NSBundle mainBundle] pathForResource:@"inkColor" ofType:@"plist"];
[fileManagerColor copyItemAtPath:bundle toPath: pathColor error:&error]; //6
}
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEEE, MMMM d, YYYY"];
NSString *dateString = [dateFormat stringFromDate:date];
[dateFormat release];
[appDelegate.inks addObject:[inkTextField text]];
[appDelegate.inkDates addObject:dateString];
[appDelegate.inkFont addObject:[inkTextField font]];
[appDelegate.inkColor addObject:[inkTextField textColor]];
NSLog(@"inkColor count: %i", [appDelegate.inkColor count]);
NSLog(@"Wrote to new index");
BOOL didWrite=[appDelegate.inks writeToFile: path atomically:YES];
if(didWrite==YES)
NSLog(@"didWrite");
else NSLog(@"nope");
BOOL didWrite2;
didWrite2=[appDelegate.inkDates writeToFile: pathDates atomically:YES];
if(didWrite2==YES)
NSLog(@"didWrite");
else NSLog(@"nope");
BOOL didWrite3;
didWrite3=[appDelegate.inkFont writeToFile: pathFont atomically:YES];
if(didWrite3==YES)
NSLog(@"didWrite");
else NSLog(@"nope");
BOOL didWrite4;
didWrite4=[appDelegate.inkColor writeToFile: pathColor atomically:YES];
if(didWrite4==YES)
NSLog(@"didWrite");
else NSLog(@"nope");
You are using writeToFile which attempts to write proper plists. Its documentation states:
UIColor and UIFont are not in the list.
Generally you might want to use the NSCoding protocol (using NSCoder objects) instead which allows you to serialize data more effectively. However, even then there is no good way to store UIFont objects – they don’t conform to NSCoding and cannot be serialized. You should store the font family/size and then have custom code to restore the font when loading the data.