NSUserDefaults NSMutable Array Not Saving Between Sessions
I have an NSMutableArray stored in NSUserDefaults. The mutable array is properly edited and the results of the edit can be viewed with:
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
However, when I quit and relaunch the application, the user defaults are back to their original form.
I try to save state by running this method to force synchronization:
-(void) saveUserDefaults{
if([[NSUserDefaults standardUserDefaults] synchronize]){
NSLog(@"Defaults saved");
}else{
NSLog(@"Defaults save failed");
}
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
}
Are there any ideas why this might not be saving?
I set the user default arrays using this code the first time the application is run:
// Check if first time run
if ([[[NSUserDefaults standardUserDefaults开发者_运维技巧] objectForKey:@"isFirstRun"] isEqualToString:@"no"]) {
// Not first run, NSUserDefault arrays are already set
}else {
// First Run of Program
// Mark for future
[[NSUserDefaults standardUserDefaults] setObject:@"no" forKey:@"isFirstRun"];
designerArray = [[NSMutableArray alloc] initWithObjects: @"Alberta Ferretti", @"Alberto Fermani", @"Alejandro Ingelmo", @"Alexander McQueen", @"Alexander Wang", @"Ann Demeulemeester", @"Anne Klein", @"Aquatalia", @"Anya Hindmarch", @"Ash", @"Badgley Mischka Platinum Label", @"Balenciaga ", @"BCBGMAXAZRIA", @"BC Studio", @"Be & D", @"Belle by Sigerson Morrison", @"Bettye Muller ", @"Beverly Feldman", @"Botkier", @"Bourne", @"Bottega Veneta", @"Brian Atwood", @"Burberry", @"Burberry Prorsum", @"Camilla Skovgaard", @"Candela", @"Carlos Falchi", @"Casadei ", @"Chie Mihara", @"Chloe", @"Christian Louboutin", @"Claudio Merazzi", @"Coach", @"Cole Haan", @"Comme des Garcons", @"Costume National", @"Cynthis Vincent", @"Delman ", @"Devi Kroell", @"Derek Lam", @"Diane von Furstenberg", @"Dior", @"Dolce & Gabbana", @"Dolce Vita", @"Donald J Pliner ", @"Donna Karan", @"Elie Tahari ", @"Elizabeth and James", @"Emilio Pucci", @"Emma Hope", @"Eric Javits", @"Fendi", @"Ferragamo ", @"Foley & Corinna", @"Frye ", @"Furla", @"Giuseppe Zanotti", @"Givenchy", @"Gucci", @"Halston Heritage", @"H by Hudson", @"Hobo International", @"House of Harlow 1960", @"Hunter", @"Jack Rogers", @"Jean Paul ", @"Jerome C. Rousseau", @"Jil Sander", @"Jimmy Choo", @"Joan & David", @"Joie", @"Jonathan Kelsey", @"Judith Leiber", @"Juicy Couture", @"Junya Watanabe", @"Kate Spade", @"Kooba", @"Kork-Ease ", @"Kors Michael Kors", @"L.A.M.B.", @"LD Tuttle", @"Le Silla", @"Lanvin", @"Lilly Pulitzer", @"Linea Pelle", @"Lockheart", @"Loeffler Randall", @"Longchamp", @"Luciano Padovan", @"Maison Martin Margiela", @"Maison Martin Margiela MM6", @"MANITOBAH MUKLUKS ", @"MANOLO BLAHNIK ", @"Marc by Marc Jacobs", @"Marc Jacobs", @"Marina Rinaldi", @"MATT BERNSON ", @"Max Kibardin", @"MCM", @"Melissa", @"Michael Kors", @"MICHAEL MICHAEL KORS", @"Milly", @"Miu Miu", @"Miss Sixty", @"Missoni", @"Modern Vintage", @"Moschino", @"Moschino Cheap and Chic", @"Nancy Gonzalez", @"Narcisco Rodriguez", @"Nina Ricci", @"Nicholas Kirkwood", @"Olivia Harris", @"Opening Ceremony", @"Oscar de la Renta", @"PATRICIA GREEN ", @"Pedro Garcia ", @"Philosophy by Alberta Ferretti", @"Pollini", @"Prada", @"Proenza Schouler", @"Pura Lopez",@"Rachel Comey ", @"Rag & Bone", @"Ralph Lauren", @"Rebecca Minkoff", @"Rene Caovilla ", @"Repetto", @"Robert Clergerie ", @"Rock & Republic", @"Rosegold", @"Rupert Sanderson", @"Salvatore Ferragamo", @"SAM EDELMAN ", @"Scorah Pattulo", @"See by Chloe", @"7 for all mankind ", @"Sergio Rossi", @"Sigerson Morrison", @"Stella McCartney ", @"Stephane Verdino", @"Steve Madden", @"Storksak", @"Stuart Weitzman", @"Studio Pollini", @"Taryn Rose", @"Thakoon", @"Theory", @"Tracy Reese", @"Tod's", @"TOMS ", @"Tory Burch", @"UGG Australia", @"Valentino", @"Vera Wang Lavender Label", @"Versace", @"Via Spiga", @"Vince Camuto", @"Yves Saint Laurent", @"Zac Posen", @"Zagliani", @"-", nil];
[[NSUserDefaults standardUserDefaults] setObject:designerArray forKey:@"designerArray"];
shoeStyles = [[NSMutableArray alloc] initWithObjects: @"Ankle Strap", @"Ballet Flat", @"Boot", @"Bootie", @"Buckle Strap", @"Caged", @"Cowboy Boot", @"D'Orsay", @"Espadrille", @"Flat", @"Flat Sandal", @"Lace Up Boot",@"Mary Jane", @"Mid Calf Boot", @"Open Toe Flat", @"Open Toe Pump", @"Platform", @"Strappy Sandal", @"Thigh High Boot", @"Rain Boot", @"Sling Back", @"T-Strap", @"-", nil];
[[NSUserDefaults standardUserDefaults] setObject:shoeStyles forKey:@"shoeStyles"];
Do you mean you set the default to an NSMutableArray and then if you change the mutable array outside of the defaults, you lose the changes?
If so, the defaults probably haven't noticed that you have changed the array. You need to create a new array and send -setObject:forKey:
again (or use the same mutable array and send -setObject:forKey:
).
By the way, the docs say you can only pass an NSArray. That doesn't mean you can't pass NSMutableArray but you need to keep to the spirit of the contract and not change the array you pass in.
When are you calling saveUserDefaults?
I would also recommend only calling [NSUserDefaults standardUserDefaults] once inside a method. Just keep a pointer to the object and use it again and again, like so:
NSMutableArray *designerArray = [[NSMutableArray alloc] initWithObjects: @"Alberta Ferretti" ...;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:designerArray forKey:@"designerArray"]; [defaults synchronize];
I used the above code with your designer array and it worked for me.
This worked for me. I pretty much had to reassign the array to itself.
[standardDefaults setObject:[standardDefaults objectForKey:@"designerArray"] forKey:@"designerArray"];
精彩评论