0
我正試圖在我的應用上使用「添加到收藏夾」功能。我似乎無法讓它正常工作。基本上每次我的手機重新啓動時,所有的收藏夾都將從數組和字典中刪除。無論如何要保存這些數據,以便每次啓動應用程序時都能保存和恢復數據。非常感謝。添加到收藏夾功能iPhone
下面是一些代碼:在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];
非常感謝所有幫助
所以我應該檢查它是否存在,如果是的話只是initWithContentsOfFile? – 2011-05-15 23:47:28
是的,類似的東西。 – omz 2011-05-16 01:06:32
您能舉出任何例子嗎? – 2011-05-16 08:20:14