2011-10-08 144 views
0

我在網站上發現了幾個與有類似問題的人有關的問題,但我仍然無法解決我的問題。這是事情:無法寫入plist文件

我正在製作一個包含兩個plist文件的iOS應用程序。當用戶第一次打開應用程序時,應用程序負責將這兩個文件移動到Documents目錄。我知道這段代碼有效,因爲我隨後可以從Documents目錄路徑中的每個文件中讀取。但是,由於明顯的未知原因,我無法寫信給他們中的任何人。下面是我用它來寫第一plist文件中的代碼:

+ (void)insertNewElementWith:(NSArray *)objects andKeys:(NSArray *)keys { 
    NSString *filePath = [self returnPathForFirstPlistFile]; 
    NSMutableDictionary *contentOfFirstPlistFile = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; 
    NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:objects forKeys:keys]; 
    [contentOfFirstPlistFile setObject:dictionary forKey:[NSNumber numberWithInt:[contentOfFirstPlistFile count]]]; 
    [contentOfFirstPlistFile writeToFile:filePath atomically:YES]; 
    [dictionary release]; 
} 

returnPathForFirstPlistFile確實工作,因爲contentOfFirstPlistFile包含{ }而非null。然而,任何人都可以向我解釋爲什麼它會一直返回{ },即使上述方法已被多次調用。

更新

這裏是我使用的文件放置在文件目錄中的代碼。 placePlistsInDocumentsDirectory只有在用戶通過應用程序登錄時調用,所以這隻會發生一次。

+ (NSString *)returnPathForFirstPlistFile { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"First.plist"]; 

    return filePath; 
} 

+ (NSString *)returnPathForSecondPlistFile { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSLog(@"%@", [paths objectAtIndex:0]); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Second.plist"]; 

    return filePath; 
} 

+ (void)placePlistsInDocumentsDirectory { 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSString *firstPlistPath = [self returnPathForFirstPlistFile]; 
    NSString *secondPlistPath = [self returnPathForSecondPlistFile]; 

    if (![fileManager fileExistsAtPath:firstPlistPath]) { 
     NSError *error; 
     NSString *bundle = [[NSBundle mainBundle] pathForResource:@"First" ofType:@"plist"]; 
     [fileManager copyItemAtPath:bundle toPath:firstPlistPath error:&error]; 
    } 

    if (![fileManager fileExistsAtPath:secondPlistPath]) { 
     NSError *error; 
     NSString *bundle = [[NSBundle mainBundle] pathForResource:@"Second" ofType:@"plist"]; 
     [fileManager copyItemAtPath:bundle toPath:secondPlistPath error:&error]; 
    } 
} 

回答

2
[NSNumber numberWithInt:[contentOfFirstPlistFile count]] 

給你一個NSNumber對象。這不是字典的有效鍵,關鍵必須是NSString。使用

[NSString stringWithFormat:@"%d",[contentOfFirstPlistFile count]] 

取而代之。但是,如果您在字典中使用數字作爲鍵,爲什麼不使用數組?

我試圖自己重現這個問題,我可以管理它的唯一方法是使用沒有字典作爲其根對象的plist。這返回了一個不是零的字典,但不允許任何修改。這不會被寫回到文件中。看看你之前的一些問題,你之前使用的是基於數組的plist,所以這可能是問題所在。

+0

謝謝,但仍然無法正常工作。當將一個BOOL變量應用到'writeToFile'行時,即使我已經用NSString替換了NSNumber,它也會返回負值。我也嘗試過使用數組,但這也不起作用。 – wstr

+0

如果您在添加「dictionary」之前和之後登錄'contentOfFirstPlistFile',您會看到什麼? – jrturton

+0

之前的空字典和內容之後的字典。 – wstr