0

我在方法中编写了这样的代码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"];
}
4

3 回答 3

0

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

NSDictionary* mydictionary = [ NSDictionary dictionaryWithContentsOfFile: pathToSettingsInBundle];
于 2012-06-30T08:01:25.200 回答
0

在 if 块内添加第二行

[fileManager copyItemAtPath:pathToSettingsInBundle toPath:path error:&error];
于 2012-06-30T08:17:53.983 回答
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,则检查您的数组或字典是否必须为空。

于 2012-06-30T10:14:10.263 回答