Вы не можете написать 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, и файл находится там. Итак, я не знаю, что происходит. –
может объяснить немного больше .. помочь .. что не сработало? – lakesh
Написание. Он не писал. Все тот же файл. –