NSUserDefaults NSMutable 数组不在会话之间保存

发布于 2024-10-01 09:12:20 字数 4419 浏览 3 评论 0原文

我有一个 NSMutableArray 存储在 NSUserDefaults 中。可变数组已正确编辑,可以使用以下命令查看编辑结果:

NSLog(@"%@", [[NSUserDefaults standardUserDefaults]dictionaryRepresentation]);

但是,当我退出并重新启动应用程序时,用户默认值恢复为原始形式。

我尝试通过运行此方法来强制同步来保存状态:

-(void) saveUserDefaults{
if([[NSUserDefaults standardUserDefaults] synchronize]){
    NSLog(@"Defaults saved");
}else{
    NSLog(@"Defaults save failed");
}

NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);

}

有什么想法可能无法保存吗?

第一次运行应用程序时,我使用以下代码设置用户默认数组:

// 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"];

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"];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久而酒知 2024-10-08 09:12:20

您的意思是您将默认值设置为 NSMutableArray,然后如果您更改默认值之外的可变数组,您会丢失更改吗?

如果是这样,默认值可能没有注意到您已经更改了数组。您需要创建一个新数组并再次发送 -setObject:forKey: (或使用相同的可变数组并发送 -setObject:forKey:)。

顺便说一句,文档说你只能传递 NSArray。这并不意味着您不能传递 NSMutableArray,但您需要遵守契约精神,不要更改传入的数组。

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.

红墙和绿瓦 2024-10-08 09:12:20

你什么时候调用 saveUserDefaults ?

我还建议仅在方法内部调用 [NSUserDefaults standardUserDefaults]。只需保留一个指向该对象的指针并一次又一次地使用它,如下所示:

NSMutableArray *designerArray = [[NSMutableArray alloc] initWithObjects: @"Alberta Ferretti" ...;

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [默认 setObject:designerArray forKey:@"designerArray"]; [默认同步];

我将上面的代码与您的设计器数组一起使用,它对我有用。

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.

深海里的那抹蓝 2024-10-08 09:12:20

这对我有用。我几乎不得不将数组重新分配给它自己。

[standardDefaults setObject:[standardDefaults objectForKey:@"designerArray"] forKey:@"designerArray"];

This worked for me. I pretty much had to reassign the array to itself.

[standardDefaults setObject:[standardDefaults objectForKey:@"designerArray"] forKey:@"designerArray"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文