如何在iphone中的plist中编写数组对象?

时间:2011-02-03 09:46:39

标签: ios objective-c iphone plist

在应用中,我有plist

root ---> dictionary
  cell ----> array
    item0 ---> string
    item1 ----> string

等等。

tableView中保存此plist&的数据。 tableview navigation bar拥有add button。当我点击它时,它会推动另一个视图,其中包含text field&一个button。我想通过单击按钮将文本字段的文本存储到最后一个对象的数组中的plist中。

每当我重新加载tableview时,数据都会永久存储在plist中。

2 个答案:

答案 0 :(得分:3)

这是代码......

-(void)dataFetching
{
NSMutableDictionary *categoryList = [[NSMutableDictionary alloc] initWithContentsOfFile:@"Give the file path"];
NSMutableArray *array =(NSMutableArray *) [categoryList valueForKey:@"write the array name here"];

/* hope u know how a table loads data from the array */

/*whenever add button is pressed on the table view cell, view would be shown with a textfield and a button, isn't it? */

/* by setting the delegate for textfield, using the textfieldDidEnterEditing: method capture the text from the textfield*/
// for example, 
NSString *testStr = @"test String"; //[textfield text];
//Now,

int lastItemIndex = [array count];
[array insertObject:testStr atIndex:lastItemIndex];
// and whenever you want you can save it on to the file like this

[categoryList writeToFile:@"Give the file path" atomically:YES];

[categoryList release];
}
希望这有助于.....

答案 1 :(得分:1)

检查方法

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag

表示NSDictionary和NSArray

以及

+ (id)arrayWithContentsOfFile:(NSString *)aPath
+ (id)dictionaryWithContentsOfFile:(NSString *)path