当我在document文件夹中手动创建plist时,我希望将Array写入plist。
但是,当我检查plist是否存在,如果不是从主包中的plist创建它时,什么都不会发生……
我是这样做的,正如在属性列表编程指南中所写的那样。
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但迪尔里没有文件!
发布于 2012-03-31 11:28:45
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];
}//***Try这个..。
发布于 2011-02-17 09:26:17
我在另一个mac上试了一下代码,它在那里工作,这是同一个项目。
有人能解释为什么这个plist是在一个mac上创建的,而不是在另一个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上。
编辑:
因为某种原因它又起作用了。
当然,我从document文件夹中删除了plist并清除了所有目标。
现在的问题和以前一样,我只能在我写字典的时候才能把它写出来,然后我只能保存最后一本。而且读夹板也没用。
有人能告诉我为什么会发生这种事吗?我怎么能解决呢?
编辑2:
我发现了问题:因为当我用字典将数组写到plist.When时,X代码不会创建plist,所以我将字典写到plist,所以X代码创建plist,但是使用字典根,所以我不能将数组写入它,也不能在数组中读取它……
我用数组根在参考资料文件夹中创建了一个plist,当它不存在时复制到document文件夹中,现在它可以工作了……
这么简单的事情怎么会让你头痛,这难道不奇怪吗?
https://stackoverflow.com/questions/5017461
复制相似问题