我有一个关于如何写一个plist的非常基本的问题.我在我的资源文件夹中添加了一个"Here.plist"文件.以下是我的代码:
NSString *path = [[NSBundle mainBundle]pathForResource:@"Here" ofType:@"plist"];
NSArray *array = [[NSArray alloc]initWithObjects:@"First", @"Second", @"Third", nil];
[array writeToFile:path atomically:YES];
Run Code Online (Sandbox Code Playgroud)
执行代码后,"Here.plist"仍为空.我甚至尝试使用NSDictionary如下:
NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:@"firstValue", @"First", @"secondValue", @"Second", @"thirdValue", @"Third", nil];
[dict writeToFile:path atomically:YES];
Run Code Online (Sandbox Code Playgroud)
我经历了很多教程和论坛,但它不适合我.我究竟做错了什么?我不知所措.
你不能写入包.尝试写入文档目录.例如:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *array = [[NSArray alloc]initWithObjects:@"First", @"Second", @"Third", nil];
[array writeToFile:[documentsDirectory stringByAppendingPathComponent:@"Here.plist" atomically:YES];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1371 次 |
| 最近记录: |