Iphone 从键数组添加到字典
在过去的几天里,我一直在努力解决以下问题 我需要在字典的字典中设置一个键和一个值。我知道字典的键,我想在那里设置一个键,但我不知道如何到达那里。 密钥存储在一个数组中 比如说,我有一个包含以下键的字典,每个键的值都是另一个字典Iphone 从键数组添加到字典,iphone,objective-c,arrays,dictionary,Iphone,Objective C,Arrays,Dictionary,在过去的几天里,我一直在努力解决以下问题 我需要在字典的字典中设置一个键和一个值。我知道字典的键,我想在那里设置一个键,但我不知道如何到达那里。 密钥存储在一个数组中 比如说,我有一个包含以下键的字典,每个键的值都是另一个字典 - myDict - A - A - A - B - B - A - B - B - A - A - B - B
- myDict
- A
- A
- A
- B
- B
- A
- B
- B
- A
- A
- B
- B
- A
- B
我还有以下数组:
[A, A, B]
这意味着,我想在a->a->B为字典中的键设置一个值。
这等于:
[[[[myDict objectForKey:@"A"] objectForKey:@"A"] objectForKey@"B"] setValue:myValue forKey:myKey]
有人知道我能做到这一点的方法吗?
我设想我应该保留对字典的引用,迭代数组中的对象,然后转到字典中的“下一级”,并保留对该对象的引用,当到达数组中的最后一个对象时,设置键的值。我的问题是,我不知道该怎么办。为了保存对字典的引用,当我进入下一个级别时,我需要初始化一个新字典,对吗?这会导致我创建一个只包含当前级别的对象的新字典,因此我不会在原始字典中设置键的值
- myDict
- A
- A
- A
- B
- B
- A
- B
- B
- A
- A
- B
- B
- A
- B
任何代码示例或伪代码都将不胜感激。您可以这样做,不要说这是“准确”的答案,但应该可以帮助您找到准确的需求
for(NSString* key in [myDict allKeys]) {
NSString *yourValue = ....
NSDictionary *dict = [[myDict objectForKey:key] objectForKey:key];// Will get you to A->A
for(NSString *levelKey in [dict allKeys]) {
//Setting Value for A->A->(A) & A->A->(B)
[dict setValue:yourValue forKey:levelKey];
}
}
这个答案应该被忽略。相反,使用@Chuck发布的方法。 我最后写了下面两种方法来解决这个问题。这是受到@nhahdh的启发。 这些方法非常适合分类,但我以前从未写过分类。总有一天我会把这些列为一个类别。如果有人想这样做,请在这里张贴结果
/**
* Sets the value in a dictionary with a keypath. Uses either the first or the last key in the keypath as the key to set the value for.
* @param value Value to set.
* @param dictionary Dictionary to set the value in.
* @param path Path for the key. Last or first object is used as the key for the value.
* @param readFromEnd Whether or not to read the path from the end.
*/
+ (void)setValue:(id)value inDictionary:(NSMutableDictionary *)dictionary withKeyPath:(NSArray *)path readFromEnd:(BOOL)readFromEnd
{
NSMutableArray *mutablePath = [NSMutableArray arrayWithArray:path];
NSString *key;
if (readFromEnd)
{
key = [mutablePath lastObject];
[mutablePath removeLastObject];
}
else
{
key = [mutablePath objectAtIndex:0];
[mutablePath removeObjectAtIndex:0];
}
[self setValue:value forKey:key inDictionary:dictionary withKeyPath:mutablePath readFromEnd:readFromEnd];
}
/**
* Sets the value in a dictionary with a keypath.
* @param value Value to set.
* @param key Key for the value.
* @param dictionary Dictionary to set the value in.
* @param path Path for the key.
* @param readFromEnd Whether or not to read the path from the end.
*/
+ (void)setValue:(id)value forKey:(NSString *)key inDictionary:(NSMutableDictionary *)dictionary withKeyPath:(NSArray *)path readFromEnd:(BOOL)readFromEnd
{
if (path.count == 0)
[dictionary setValue:value forKey:key];
else
{
NSMutableArray *mutablePath = [NSMutableArray arrayWithArray:path];
NSString *currentKey;
if (readFromEnd)
{
currentKey = [mutablePath lastObject];
[mutablePath removeLastObject];
}
else
{
currentKey = [mutablePath objectAtIndex:0];
[mutablePath removeObjectAtIndex:0];
}
[self setValue:value forKey:key inDictionary:[dictionary objectForKey:currentKey] withKeyPath:mutablePath readFromEnd:readFromEnd];
}
}
在我看来,您只需执行
[yourArray componentsJoinedByString:@.”]
来获取一个键路径,然后使用valueForKeyPath:
来获取适当的值。您尝试过递归吗?(我不认为嵌套太深,但即使如此,也有可能将递归转换为带堆栈的循环)我不知道新字典将在哪一点初始化…?这正是我一直在寻找的!轻松优雅。非常感谢你。