无法在文档文件夹中创建plist文件

时间:2012-06-30 07:22:46

标签: iphone

我在Viewdidload方法中编写了这样的代码。但它不会创建文件.plist

我不知道代码是wi8还是错误。我必须写的地方?

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory =  [paths objectAtIndex:0]; //2
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data1.plist"]; //3
NSFileManager *fileManager = [NSFileManager defaultManager];

if ( ![fileManager fileExistsAtPath:path] ) {
    NSString *pathToSettingsInBundle = [[NSBundle mainBundle] 
    pathForResource:@"data1" ofType:@"plist"];
}

3 个答案:

答案 0 :(得分:0)

您刚刚创建了一个路径,您应该尝试将其加载到NSDictionary中。

NSDictionary* mydictionary = [ NSDictionary dictionaryWithContentsOfFile: pathToSettingsInBundle];

答案 1 :(得分:0)

在if块内添加第二行

[fileManager copyItemAtPath:pathToSettingsInBundle toPath:path error:&error];

答案 2 :(得分:0)

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *documentsDirectory = [paths objectAtIndex:0]; 

plistpath = [[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:@"Product.plist"]];

然后

[array writeToFile:path atomically:YES]; 

[Whatever [Array or Dictionary] you want to Write in Plist]

如果仍未创建plist,请检查您的数组或字典是否必须为空。