2012-06-30 181 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然後檢查是否您的數組或字典必須空着。