2013-04-11 87 views
1

我有一個方法返回一串NSMutableArray的字符串。我怎樣才能將這個數組(包含字符串)添加到plist文件並使用它來填充UITableView?由於寫數組到plist並填充tableview

+1

[你嘗試過什麼?](http://mattgemmell.com/2008/12/08/what-have-you-tried /) – 2013-04-11 14:16:02

+0

NSString * path = [[NSBundle mainBundle] pathForResource:@「brochurenames」ofType:@「plist」]; [[self returnBrochureNames] writeToFile:path:atom]; <成功,但我似乎無法從中讀取 – Daantjeeuh 2013-04-11 14:18:18

回答

9

創建值

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

創建文檔目錄文件路徑的數組,你可以在那裏寫的文件,而不是在應用程序捆綁

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

保存數組到plist文件

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

從plist讀取數組使用以下代碼

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

但如果你仍然沒有得到你可以參考教程here

+1

太棒了,這真的幫助了我!謝謝!編輯:你應該在文件名前添加一個「/」來實際寫入文件夾。 – Daantjeeuh 2013-04-12 07:18:29