我正在尝试在我的应用程序上使用“添加到收藏夹”功能。我似乎无法让它正常工作。基本上每次我的手机重启时,所有收藏夹都会从数组和字典中删除。有没有办法保存这些数据,以便在每次应用启动时保存和恢复?非常感谢。
这是一些代码:在 appDidFinishLaunching 中:
//============== Add To Favourites ==============
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Saved.data"];
NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryArray = [pathsArray objectAtIndex:0];
NSString *filePathArray = [documentsDirectoryArray stringByAppendingPathComponent:@"savedArray.data"];
delegateFavouritesDictionary = [NSMutableDictionary dictionary];
[delegateFavouritesDictionary writeToFile:filePath atomically:YES];
delegateFavouritesArray = [[NSMutableArray alloc]init];
在 detailViewController viewDidLoad 中:
self.addToFavouritesArray = [[NSMutableArray alloc] init];
self.addToFavouritesDictionary = [NSMutableDictionary dictionary];
TabBar_NavigationBasedAppDelegate *mainDelegate = (TabBar_NavigationBasedAppDelegate *)[[UIApplication sharedApplication]delegate];
//addToFavouritesArray = [[NSMutableArray alloc] init];
NSMutableArray *tempArray1 = mainDelegate.delegateFavouritesArray;
//NSMutableDictionary *tempDictionary1 = mainDelegate.delegateFavouritesDictionary;
addToFavouritesArray = tempArray1;
//self.addToFavouritesDictionary = tempDictionary1;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Saved.data"];
addToFavouritesDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
在 detailViewController 中,在 addToFavourites 函数中:
NSString *ID = [[NSUserDefaults standardUserDefaults]objectForKey:@"ID"];
if([[addToFavouritesDictionary allKeys] containsObject:ID]) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Saved.data"];
[addToFavouritesDictionary removeObjectForKey:ID];
[addToFavouritesArray removeObject:Name];
[favouritesButton setTitle:@"+ Favourites" forState:(UIControlState)UIControlStateNormal];
[addToFavouritesDictionary writeToFile:filePath atomically: YES];
NSLog(@"New Dictionary: %@", addToFavouritesDictionary);
} else {
[addToFavouritesArray addObject:Name];
NSString *ID = [[NSUserDefaults standardUserDefaults]objectForKey:@"ID"];
[addToFavouritesDictionary setObject:Name forKey:ID];
[favouritesButton setTitle:@"- Favourites" forState:(UIControlState)UIControlStateNormal];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Saved.data"];
[addToFavouritesDictionary writeToFile:filePath atomically: YES];
NSLog(@"Mutable Dictionary: %@", addToFavouritesDictionary);
//[addToFavouritesDictionary release];
}
在 FavouritesViewController 中,在 viewDidLoad 中:
TabBar_NavigationBasedAppDelegate *mainDelegate = (TabBar_NavigationBasedAppDelegate *)[[UIApplication sharedApplication]delegate];
favouritesArray = [[NSMutableArray alloc] init];
NSMutableArray *tempArray1 = mainDelegate.delegateFavouritesArray;
favouritesArray = tempArray1;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Saved.data"];
favouritesDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
非常感谢您的帮助