xcode 如何使用 NSUserDefaults 更新密钥的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5391510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:36:40  来源:igfitidea点击:

How can I update data for a key using NSUserDefaults

iphoneobjective-ccocoaxcodensuserdefaults

提问by Csabi

Hi i want to update data for key but it still stays the same my code is:

嗨,我想更新密钥的数据,但它仍然保持不变,我的代码是:

NSUserDefaults *userNamePrefs = [NSUserDefaults standardUserDefaults];

    [userNamePrefs removeObjectForKey:@"mykey"];

    [userNamePrefs setObject:@"myNewStringValue" forKey:@"mykey"];

but it does not works it does not remove object and ignore to add new value for key, please help what am i missing

但它不起作用它不会删除对象并忽略为键添加新值,请帮助我缺少什么

thanks

谢谢

回答by Vladimir

Try calling

试试打电话

[userNamePrefs synchronize];

after you made your updates

在您进行更新后