[英]how to use setObject: forKey where Key is also a NSMutableDictionary?
I have a NSMutableDictionary
inside another NSMutableDictionary
how should i set a value inside the inner NSMutableDictionary
, I have tried 我有一个NSMutableDictionary
另一个里面NSMutableDictionary
我应该如何设置内部内的值NSMutableDictionary
,我曾尝试
[[PUser setObject:self.name forKey:@"profile"][@"name"];
but it gives subscript requires size of interface'NSString',which is not consistent with this architecture and platform error,any suggestions? 但是它给出了下标要求接口'NSString'的大小,这与该体系结构和平台错误不一致,有什么建议吗?
lets say your dictionary structure is like this : 可以说您的字典结构是这样的:
mainDictionary = {
"innerDictionary" = {
"profile":"somename",
"key2":"someValue",
"key3":"someValue"
}
}
1) Now if you want to set value of for key " profile
" which is inside innerDictionary
then you can set it as follows: 1)现在,如果要在innerDictionary
内部设置键“ profile
”的值 ,则可以如下设置:
[mainDictionary setValue:yourValue forKeyPath:@"innerDictionary.profile"];
2) if you want object
to be saved then do this: 2)如果要保存object
,请执行以下操作:
[[mainDictionary valueForKey:@"innerDictionary"] setObject:yourValue forKey:@"profile"];
Hope this will help you. 希望这会帮助你。
尝试以下代码:
[[yourMainDic_Name objectForKey:@"innerDic_KeyName"] setValue:@"yourVlaue" forKey:@"KeyName"]; // you can also change as per your requirement to "setObject" instead of "setValue"
您想要向现有密钥添加新密钥,然后首先使用objectForKey:@"FirstKey"
,然后根据需要设置New Value和NewKey。
[[Dic objectForKey:@"name"] setObjcet:self.name forKey:@"profile"];
NSMutableDictionary *dictmain,*dictsub;
[dictmain setObject:dictsub forkey:@"dictsub"];
// This might be how a dictionary object added to another dictionary . //这可能是将字典对象添加到另一个字典的方式 。
NSMutableDictionary *innerDict = [dictmain objectforKey:@"dictsub"];
[innerdict setObject:customObj forKey@"new key"];
// This will insert a new object to the inner dictionary. //这会将一个新对象插入内部字典。 Also you can use setValue method. 您也可以使用setValue方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.