简体   繁体   English

如何使用setObject:forKey其中Key也是NSMutableDictionary?

[英]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.

相关问题 setObject:forKey:和setValue:forKey:在NSMutableDictionary中的区别在哪里? - Where's the difference between setObject:forKey: and setValue:forKey: in NSMutableDictionary? 无法在NSMutableDictionary中设置setObject:forKey: - Unable to setObject:forKey: in NSMutableDictionary NSDictionary 可能无法响应 NSMutableDictionary object 上的 setObject:forKey - NSDictionary may not respond to setObject:forKey on a NSMutableDictionary object iPhone NSMutableDictionary setObject forKey initWithContentsOfFile之后 - iPhone NSMutableDictionary setObject forKey after initWithContentsOfFile NSMutableDictionary SetObject:ForKey:未插入NSDictionary对象 - NSMutableDictionary SetObject:ForKey: is not inserting NSDictionary object 图片为空[NSCFDictionary setObject:forKey:]:尝试插入nil值(键:) - Image is null [NSCFDictionary setObject:forKey:]: attempt to insert nil value (key:) NSUserDefaults setObject和IntegerForKey,在同一密钥上使用,如果没有,我该如何解决? - NSUserDefaults setObject and IntegerForKey, use on the same key, if not how can I workaround? 使用NSDictionary setObject:forKey时的SIGABRT - SIGABRT when using NSDictionary setObject: forKey: NSUserDefaults standardUserDefaults setObject:forKey:不适用于多值首选项 - NSUserDefaults standardUserDefaults setObject: forKey: not working for Multivalue preference 如何在NSMutableDictionary中重命名密钥? - How to rename a Key in NSMutableDictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM