2011-10-27 100 views
0

我正在向NSArray添加一個NSString,並在另一個Tableview中讀取它,該工作完美無瑕。將NSArray添加到.plist

在我的DetailView:

NSString *documentDirectory = [self applicationDocumentsDirectory]; 
    NSString *path = [documentDirectory stringByAppendingPathComponent:@"Favs.plist"]; 

    NSArray *Array = [[NSArray alloc] initWithObjects:self.title, nil]; 

    [Array writeToFile: path atomically:YES]; 

在我FavTableView:

NSString *documentDirectory = [self applicationDocumentsDirectory]; 
NSString *path = [documentDirectory stringByAppendingPathComponent:@"Favs.plist"]; 

tempArray = [[NSArray alloc] initWithContentsOfFile:path]; 

array = [[NSMutableArray alloc] initWithArray:tempArray copyItems:YES]; 

NSLog(@"array is: %d", [array count]); 

但是,如果我想加載,在不同的詳細信息視圖,另一個字符串我的數組,它會覆蓋舊值。我如何避免這種情況?

回答

0

你會打電話來創建NSDictionary或NSArray,它將包含您的數組並寫入文件的全局字典或數組。如果您需要逐個添加數組,請執行以下步驟: 1.使用現有文件的內容初始化Dictionary 2.創建字典的可變副本,在步驟#1創建 3.添加新數組的字典 4.寫更新字典文件

如果全球陣列更好地滿足您的需求超過全球的詞典裏,步驟是相同的​​,但使用數組而不是字典=)