iOS_User
iOS_User

Reputation: 1382

How to write array object in plist in iphone?

In an app I have a plist:

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

and so on.

In tableView which holds the data from this plist & tableview navigation bar holds an add button. When I clicked on it, it pushes another view which holds a text field & a button. I want to store the text field's text into the plist in an array at the last object by clicking on the button.

Whenever I reload the tableview, the data is stored in the plist permanently.

Upvotes: 1

Views: 5834

Answers (2)

Gina
Gina

Reputation: 132

here is the code....

-(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];
}

hope this helps.....

Upvotes: 3

Max
Max

Reputation: 16709

Check the methods

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

for NSDictionary and NSArray

and also

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

Upvotes: 1

Related Questions