2011-08-21 96 views
1

我有4個NSMutableArray,啓動時它們已正確初始化。在我的代碼的一部分中,我將這些數組的內容寫入各個plist文件,並且這些plist文件已創建並添加到項目中。我有4個寫入文件的實例,出於某種原因,只有前2個寫入才能成功(didWrite和didWrite2),但寫入3和寫入4卻始終返回「nope」,這意味着寫入失敗。下面是初始化和的applicationDidFinishLaunching分配數組的代碼:爲什麼我在嘗試寫入plist文件時出現故障

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"); 

回答

1

您正在使用將writeToFile它試圖寫正確的Plist。它的文檔指出:

這種方法遞歸地驗證了所有包含的對象是 屬性列表對象寫出來的文件之前(NSData的,NSDate的,的NSNumber, 的NSString,NSArray的,或者NSDictionary的實例),和 回報如果所有對象都不是屬性列表對象,則爲NO,因爲 生成的文件不是有效的屬性列表。

如果字典的內容都是屬性列表對象,此方法寫入文件 可以用來初始化一個新的字典, 類方法dictionaryWithContentsOfFile:或實例方法 initWithContentsOfFile :.

UIColor和UIFont不在列表中。

一般而言,您可能希望使用NSCoding協議(使用NSCoder對象),而不是使用NSCoding協議來更有效地序列化數據。然而,即使這樣,也沒有好的方法來存儲UIFont對象 - 它們不符合NSCoding並且不能被序列化。您應該存儲字體系列/大小,然後在加載數據時使用自定義代碼來恢復字體。

相關問題