我有4个NSMutableArray,并且在启动时它们已正确初始化。在我的代码的一部分中,我将这些数组的内容写入单个plist文件,并且已创建这些plist文件并将其添加到项目中。我有4个写入文件的实例,由于某种原因,只有前2个写入文件成功(didWrite和didWrite2)但didWrite3和didWrite 4一直返回“nope”意味着写入不成功。下面是在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");
当应用程序首次启动时,我将“初始化的inkColor视为新的”,但我仍然无法编写。墨水和墨水日期写得不错,但其他两个不合适。这是我的代码试图写入plist文件:
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");
答案 0 :(得分:1)
您正在使用writeToFile尝试编写正确的plist。其文件说明:
此方法以递归方式验证所有包含的对象是否为 属性列表对象(NSData,NSDate,NSNumber, NSString,NSArray或NSDictionary)在写出文件之前,和 如果所有对象都不是属性列表对象,则返回NO,因为 结果文件不是有效的属性列表。
如果字典的内容都是属性列表对象,则该文件 通过此方法编写的可用于初始化新字典 类方法dictionaryWithContentsOfFile:或实例方法 initWithContentsOfFile:
UIColor和UIFont不在列表中。
通常,您可能希望使用NSCoding协议(使用NSCoder对象),这样可以更有效地序列化数据。然而,即使这样,也没有好的方法来存储UIFont对象 - 它们不符合NSCoding并且无法序列化。您应该存储字体系列/大小,然后使用自定义代码在加载数据时恢复字体。