Iphone 创建plist文件
我想在文档文件夹中手动创建plist时,向plist写入一个数组就可以了 但是,当我检查plist是否存在,以及如果不从主捆绑包中的plist创建它时,什么都没有发生 我是按照书上写的那样做的Iphone 创建plist文件,iphone,plist,Iphone,Plist,我想在文档文件夹中手动创建plist时,向plist写入一个数组就可以了 但是,当我检查plist是否存在,以及如果不从主捆绑包中的plist创建它时,什么都没有发生 我是按照书上写的那样做的 NSString *plistRootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *plistPath = [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
但是目录中没有文件 我在另一台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上
编辑:
出于某种原因,它又起作用了
为了确保我从文档文件夹中删除了plist并清除了所有目标
现在的问题和以前一样,我只能在写字典的时候写plist,但是我只能保存最后一个。
而阅读plist也不管用
有人能告诉我为什么会发生这种情况以及我如何解决它吗
编辑2:
我发现了这个问题:因为X-Code在我用字典将数组写入plist时不会创建plist。当我将字典写入plist时,X-Code创建了plist,但使用了字典根,因此我无法将数组写入plist,也无法在数组中读取它
我在Resources文件夹中创建了一个具有数组根的plist,并在它不存在时将其复制到document文件夹,现在它可以工作了
这么简单的事情会让你头疼,这不是很奇怪吗
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];
}
//****试试这个…缺少实际创建阵列的代码?类似于array=[NSArray arrayWithContentsOfFile:plistPath]?您是在模拟器中还是在设备上测试它?你能在这里记录plistPath并粘贴吗?你需要添加更多信息。尝试添加plistPath的一些NSLog并发布输出。你确定“list.plist”文件在你的XCode项目中吗?数组在那里并且充满了数据。。。我在模拟器中测试了它,当然你是对的,我应该检查它们的plistPath。。。。。文件(如果存在)的路径是:
2011-02-16 15:21:31.485 ListApp[2782:207]…/Library/Application Support/iPhone Simulator/4.0.2/Applications/267F55E4-A46A-42E0-9E1C-EAF26846F75F/Documents/list.plist
文件(如果存在)的路径!存在的是:2011-02-16 15:21:31.486 ListApp[2782:207]../Library/Application Support/iPhone Simulator/4.0.2/Applications/267F55E4-A46A-42E0-9E1C-EAF26846F75F/ListApp.app/list.plist
如何修复?