2012-06-30 225 views
0

我在Viewdidload方法中编写了类似这样的代码。但它不会创建文件.plist无法在文档文件夹中创建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"]; 
} 

回答

0

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

NSDictionary* mydictionary = [ NSDictionary dictionaryWithContentsOfFile: pathToSettingsInBundle]; 
0

里面你如果块添加第二行

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

我得到一个错误 - [NSFileManager copyItemAtPath:toPath:error:]:源代码路径,如果我添加这一行。 –

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然后检查是否您的数组或字典必须空着。