ios 如何更改NSdictionary 中的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21772198/
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
how change values in NSdictionary?
提问by catalunya
How change value "level 1-2 closed" on "level 1-2 open"? I have to delete it and add new value? or simply overwrite? How can I do it?
如何在“1-2 级打开”上更改值“1-2 级关闭”?我必须删除它并添加新值?还是简单地覆盖?我该怎么做?
Thanks
谢谢
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"key_open_levels"];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"open", @"level 1-1",
@"closed", @"level 1-2",
@"closed", @"level 1-3",
@"closed", @"level 1-4",
@"closed", @"level 1-5",
@"closed", @"level 1-6",
@"closed", @"level 1-7",
@"closed", @"level 1-8",
@"closed", @"level 1-9",
@"closed", @"level 1-10", nil];
// NSString *customerDict ;
[[NSUserDefaults standardUserDefaults] setValue:dict forKey:@"key_open_levels"];
if ([[dict valueForKey:@"level 1-2"] isEqualToString:@"closed" ]){
//what should I write here?
}else{
}
回答by Mani
Try below code..
试试下面的代码..
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"open", @"level 1-1",
@"closed", @"level 1-2",
@"closed", @"level 1-3",
@"closed", @"level 1-4",
@"closed", @"level 1-5",
@"closed", @"level 1-6",
@"closed", @"level 1-7",
@"closed", @"level 1-8",
@"closed", @"level 1-9",
@"closed", @"level 1-10", nil];
[dict removeObjectForKey:@"level 1-2"];
[dict setObject:@"open" forKey:@"level 1-2"];
回答by iPatel
If you need to add your entire NSDictionary
to NSMutableDictionary
如果您需要将整个添加NSDictionary
到NSMutableDictionary
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
[newDict addEntriesFromDictionary: dict];
[newDict setObject:@"<value>" forKey:@"<Key Name>"];
NSLog(@"%@", newDict);
回答by codercat
NSDictionary is immutable so can't change the value. NSMutableDictionary is mutable then only you add,delete performes can be done.
NSDictionary 是不可变的,所以不能改变它的值。NSMutableDictionary 是可变的,只有你添加,删除执行才能完成。
use @Amar said link . it helpful
使用@Amar 说的链接。它有帮助
回答by rahul
In your code below [[NSUserDefaults standardUserDefaults] setValue:dict forKey:@"key_open_levels"];
Use below line of code than it will work.
[defaults synchronize];
Also use NSMutableDictionary instead of NSDictionary.
也使用 NSMutableDictionary 而不是 NSDictionary。
回答by Rigel Networks
You have to convert NSDictionary
to NSMutableDictionary
.
您必须转换NSDictionary
为NSMutableDictionary
.
Or
或者
You have to user NSMutableDictionary
in place of the NSDictionary
.
您必须使用 userNSMutableDictionary
代替NSDictionary
.
After that you can able to change value in NSMutableDictionary
.
之后,您可以更改NSMutableDictionary
.
NSMutableDictionary *mutableDict = [dict mutableCopy];
回答by NeverHopeless
Apple provides us NSMutableDictionary
and NSDicitionary
to store data in key-value pair, in which NSMutableDicitonary
is editable. It depends on the use that if the data will be alter you should use NSMutableDictionary
instead of creating NSDictionary
or [instanceOfNSDictionary mutableCopy]
苹果为我们提供了NSMutableDictionary
与NSDicitionary
在键值对存储数据,其中NSMutableDicitonary
可编辑。这取决于使用,如果数据将被更改,您应该使用NSMutableDictionary
而不是创建NSDictionary
或[instanceOfNSDictionary mutableCopy]
For example:
例如:
NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"open", @"level 1-1",
@"closed", @"level 1-2",
@"closed", @"level 1-3",
@"closed", @"level 1-4",
@"closed", @"level 1-5",
@"closed", @"level 1-6",
@"closed", @"level 1-7",
@"closed", @"level 1-8",
@"closed", @"level 1-9",
@"closed", @"level 1-10", nil];
[dict2 setObject:@"open" forKey:@"level 1-2"]; //<---- Set value at later stage
EDIT
编辑
// Archive data to save
NSMutableArray *archiveArray = [NSMutableArray array];
NSData *personEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:completeResponse];
[archiveArray addObject:personEncodedObject];
// Set in userDefault
[[NSUserDefaults standardUserDefaults] setValue:archiveArray forKey:@"key_open_levels"];
[[NSUserDefaults standardUserDefaults] synchronize];
// Get archived data from userdefault
archiveArray = [[NSUserDefaults standardUserDefaults] valueForKey:@"key_open_levels"];
// Unarchive and use it
NSLog(@"%@", [NSKeyedUnarchiver unarchiveObjectWithData:[archiveArray objectAtIndex:0]]);
回答by gajchtr
you can do like this.. nsdictionary is static. we can't set object once it is created. take it as a nsmutabledictionary . you can chage it. NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
你可以这样做.. nsdictionary 是静态的。一旦创建对象,我们就无法设置它。把它当作一个 nsmutabledictionary 。你可以改变它。NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"open", @"level 1-1",
@"closed", @"level 1-2",
@"closed", @"level 1-3",
@"closed", @"level 1-4",
@"closed", @"level 1-5",
@"closed", @"level 1-6",
@"closed", @"level 1-7",
@"closed", @"level 1-8",
@"closed", @"level 1-9",
@"closed", @"level 1-10", nil];
[dict setObject:@"open" forKey:@"level 1-5"];
NSLog(@"%@",dict);