你不能你的plist寫信給你需要使用NSDocumentDirectory
或NSCachesDirectory
捆只要複製的plist捆紮覆蓋它。
注:學習NSCachesDirectory
和NSDocumentDirectory
https://developer.apple.com/icloud/documentation/data-storage/
之間的區別你的plist從包複製到文件(在下面的代碼緩存),你需要這個只有一次,如果你在你的包plist中,我更喜歡使用這個代碼appdelegate.m時- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Names.plist"];
NSString *[email protected]"Names.plist";
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:plistInDocuments];
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){
[[NSFileManager defaultManager] copyItemAtPath:sourcePath
toPath:dataPath
error:&error];
}
NSLog(@"Error description-%@ \n", [error localizedDescription]);
NSLog(@"Error reason-%@", [error localizedFailureReason]);
讓您的文件並覆蓋其
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *[email protected]"Names.plist";
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:plistInDocuments];
//add object here
NSMutableArray *namesNew = [[NSMutableArray alloc] initWithContentsOfFile:dataPath];
[namesNew addObject:arrayValues];
NSError *error = nil;
if ([myFile writeToFile:dataPath options:NSDataWritingAtomic error:&error]) {
// file saved
} else {
// error writing file
NSLog(@"Unable to write plist to %@. Error: %@", dataPath, error);
}
沒有工作。我瞭解你的方法,但不知道爲什麼它不起作用。我記錄了'documentsDirectory',它將正確的路徑返回到Names.plist,並且文件在那裏。所以,我不知道發生了什麼。 – 2013-05-13 16:40:06
可以解釋多一點..幫助..什麼都行不通? – lakesh 2013-05-13 16:42:57
寫作。它沒寫。仍然是同一個文件。 – 2013-05-13 16:44:00