你不能你的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