创建一个plist文件

问题描述:

我想写一个数组到一个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"]; 

} 

我错过了什么?

编辑:

我一个单独的应用程序tryed的功能和它的工作。 所以我再次tryed它:

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 

但在迪尔没有文件!

+0

缺码,你实际上创建阵列?像数组= = [NSArray arrayWithContentsOfFile:plistPath]? – Vladimir 2011-02-16 14:15:42

我tryed上的另一台Mac上的代码,有它的工作原理这是同一个项目。

任何人都可以解释为什么plist是在一个mac上创建的,而不是在另一个上创建的?

在iPhone上也没有plist写的?

我在这两种情况下想这是导致我写的plist数组为空:

NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"list.plist"]; 
NSMutableArray *currentArray = [[NSMutableArray alloc] initWithContentsOfFile:plistPath]; 
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
      [selectet valueForKey:NAME], @"name", 
      nil]; 

NSLog(@" DIC. : %@", dictionary); 
[currentArray addObject:[dictionary copy]]; 
NSLog(@" %@", currentArray); 
[currentArray writeToFile:plistPath atomically:YES]; 
[currentArray release]; 

词典中的条目,但currentArray是空的.... 而且还是它的工作原理上的一个MAC 。但不在另一方面和iPhone上。

编辑:

出于某种原因,一切又恢复了。

为了确保我从文档文件夹中删除了plist并清除了所有目标。

而现在的问题,因为之前我当我写字典中只能写的plist但当时我只能保存最后一个。 阅读plist也无济于事。

谁能告诉我为什么发生这种情况,我该如何解决它?

编辑2:

我发现这个问题:因为当我写的阵列字典到plist.When X-代码不会创建plist中我写的字典plist中那么X -Code创建的plist,但使用一本字典的根,所以我不能在阵列写它,我也不能以阵列根数组中的阅读.....

我的资源文件夹中创建的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]; 
} 

// * * *尝试......