【问题标题】:Write array to plist and fill tableview将数组写入 plist 并填充 tableview
【发布时间】:2013-04-11 14:14:32
【问题描述】:

我有一个方法可以返回一个带有一堆字符串的 NSMutableArray。如何将此数组(带字符串)添加到 plist 文件并使用它来填充 UITableView?谢谢

【问题讨论】:

  • NSString *path = [[NSBundle mainBundle] pathForResource:@"brochurenames" ofType:@"plist"]; [[self returnBrochureNames] writeToFile:path atomically:YES];

标签: objective-c xcode uitableview nsmutablearray plist


【解决方案1】:

用值创建一个数组

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

【讨论】:

  • 太好了,这对我很有帮助!谢谢!编辑:您应该在文件名前添加一个“/”以将其实际写入文档文件夹中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多