Bal*_*ala 4 iphone objective-c nsmutablearray nsmutabledictionary
for(Attribute* attribute in appDelegate.attributeArray) {
attribute = [appDelegate.attributeArray objectAtIndex:z];
attri = attribute.zName;
int y = 0;
for(Row* r in appDelegate.elementsArray) {
r = [appDelegate.elementsArray objectAtIndex:y];
NSString *ele = r.language;
if([attri isEqualToString:ele]) {
NSLog(@"=================+++++++++++++++++%@ %@",attri, r.user);
[aaa insertObject:r atIndex:y]; //here i am adding the value to array
[dict setObject:aaa forKey:attri]; //here i am adding the array to dictionary
}
y++;
}
z++;
NSLog(@"============$$$$$$$$$$$$$$$$$$$$$$$++++++++++ %@",dict);
}
Run Code Online (Sandbox Code Playgroud)
键入一个数组,另一个数组中的值和值数组是对象格式.
我需要为单个键存储多个对象.在attributeArray有键值和elementsArray拥有的对象.例如,attributeArray可能具有值
" English, French, German..."
Run Code Online (Sandbox Code Playgroud)
而elementsArray可能具有对象值
"<Row: 0x4b29d40>, <Row: 0x4b497a0>, <Row: 0x4e38940>, <Row: 0x4b2a070>, <Row: 0x4b29ab0>, <Row: 0x4b178a0> "
Run Code Online (Sandbox Code Playgroud)
在第一个值中,我需要存储两个对象,对于第二个键,我需要存储3个对象,而第三个键需要在字典中存储最后两个对象.
对于超简化,您可以使用以下代码:
NSArray *keys = ...;
NSArray *values = ...;
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjects: values forKeys: keys];
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
更新:
要在字典中存储单个键的多个值,只需使用NSArray/ NSMutableArray作为您的对象:
NSArray *keys = ...;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for( id theKey in keys)
{
NSMutableArray *item = [NSMutableArray array];
[item addObject: ...];
[item addObject: ...];
...
[dict setObject: item forKey: theKey];
}
Run Code Online (Sandbox Code Playgroud)
如果您从一开始就不知道密钥的所有值,并且需要逐个添加它们,则可以使用以下方法:
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for( /*some cycling condition here */)
{
id keyToUse = ...;
id valueToAdd = ...;
id foundArray = [dict objectForKey: keyToUse];
if ( nil == foundArray )
{
foundArray = [NSMutableArray array];
[dict setObject: foundArray forKey: keyToUse];
}
[foundArray addObject: valueToAdd];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23455 次 |
| 最近记录: |