user2061153
user2061153

Reputation:

Write array to plist and fill tableview

I have a method that returns a an NSMutableArray with a bunch of strings. How can I add this array (with strings) to a plist file and use it to fill an UITableView with it? Thanks

Upvotes: 2

Views: 4618

Answers (1)

Dipen Panchasara
Dipen Panchasara

Reputation: 13600

Create an array with Values

NSArray *array = [NSArray arrayWithObjects:@"One",
                  @"Two",
                  @"Three",
                  @"Four", nil];

Create File Path in Document Directory, you can write file there, not in Application Bundle

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolder = [path objectAtIndex:0];
NSString *filePath = [documentFolder stringByAppendingFormat:@"myfile.plist"];

Save Array to Plist file

[array writeToFile:filePath atomically:YES];
NSLog(@"file Stored at %@",filePath);

To Read Array From plist Use Following Code

NSArray *plistArray = [NSArray arrayWithContentsOfFile:filePath];
NSLog(@"%@",plistArray);

Though if you are still not getting you can refer tutorial here

Upvotes: 8

Related Questions