2012-08-23 159 views
0

我试图取代我NSDictionnary值,但我无法达到我要找的rootObj replaceObjectAtIndex,我怎样才能得到valueForKey?

的一部分,我想用这种方法,还是那种......

我有一个NSDictionnary和我想替换此:

[[rootObj objectAtIndex:0] valueForKey:@ “检查”]

有此方法:

rootObj replaceObjectAtIndex:<#(NSUInteger)#> withObject:<#(ID)#>

,但我不能存取权限的valueForKey ...我怎样才能达到目标?

谢谢!

+0

rootObj a mutableArray?它包含字典? –

回答

0

使用此代码替换代码

编辑:更新根据用户匹配回答

NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init]; 
NSDictionary *oldDict = (NSDictionary *)[rootObj objectAtIndex:0]; 
[newDict addEntriesFromDictionary:oldDict]; 
[newDict setObject:@"1" forKey:@"check"]; 
[rootObj replaceObjectAtIndex:0 withObject:newDict]; 

在上面的例子中,我想替换@“Sales Qty”的对象 ,所以我创建了字典并在那些字典中设置了对象,然后只设置@“Sales Qty”键的值并替换整个字典in array

如果还有问题,请告知我

+0

感谢的人,我用它,工作完美,但我修改的东西,因为我并不需要的,如果:) 使用此: NSMutableDictionary * newDict = [[NSMutableDictionary alloc] init]; NSDictionary * oldDict =(NSDictionary *)[rootObj objectAtIndex:0]; [newDict addEntriesFromDictionary:oldDict]; [newDict setObject:@“1”forKey:@“check”]; [rootObj replaceObjectAtIndex:0 withObject:newDict]; 谢谢! – xGoPox

1

首先获得所有钥匙是这样的:

NSArray *arrKeys = [yourDictionary allKeys]; 

现在更换这样说ü要更换第二个对象:

if([arrKeys objectAtIndex:1]) //key for second Object 
{ 
    [yourDictionary setObject:yourNewValueHere forKey:[arrKeys objectAtIndex:1]]; //it overwrites value 
}