iphone - 创建一个 plist 文件

标签 iphone plist

我想将数组写入 plist,当我在文档文件夹中手动创建 plist 时效果很好。

但是当我检查 plist 是否存在并且如果不从主包中的 plist 创建它时,什么也不会发生......

我按照 Property List Programming Guide 那样做了这个已经写完了。

NSString *plistRootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [plistRootPath stringByAppendingPathComponent:@"list.plist"];

if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {

    plistPath = [[NSBundle mainBundle] pathForResource:@"list" ofType:@"plist"];

}

我错过了什么?

编辑:

我在一个单独的应用程序中尝试了该功能,并且它有效。 所以我又试了一次:

 NString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
 NSString *plistPath = [rootPath stringByAppendingPathComponent:@"list.plist"]; 

self.ListArray = [NSMutableArray arrayWithContentsOfFile:plistPath]; 
NSLog(@"preSave: %@", [ListArray count]);
NSLog(@"%@", plistPath);

日志显示:

2011-02-16 16:54:56.832 ListApp[3496:207] .../Library/Application Support/iPhone Simulator/4.0.2/Applications/267F55E4-A46A-42E0-9E1C-EAF26846F75F/Documents/list.plist

但是目录中没有文件!

最佳答案

NSString *DocPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

filePath=[DocPath stringByAppendingPathComponent:@"AlarmClock.plist"];

if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
{
    NSString *path=[[NSBundle mainBundle] pathForResource:@"AlarmClock" ofType:@"plist"];
    NSLog(@"file path: %@",filePath);
    NSDictionary *info=[NSDictionary dictionaryWithContentsOfFile:path];
    [info writeToFile:filePath atomically:YES];
}

//***试试这个...

关于iphone - 创建一个 plist 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5017461/

相关文章:

iphone - Plist 到 UItableview

objective-c - NSArray 来自 NSDictionary 中的数组

ios - 在 iOS 上根据 URL 生成文件名?

iPhone 随机数附加 URL

ios - JSON 解析在 IOS 中获取错误 NSErrorFailingURLKey

ios - 以编程方式对齐 iPhone 键盘顶部的工具栏

iphone - 如何将 JSON 文件从 Web 服务保存到本地 JSON 文件?

iPhone - 从视频文件中分离音频并将其保存到单独的文件中

Apple PropertyList-1.0.dtd 的 java 库

ios - 在 Plist 中解码嵌套字典