[英]Fetch NSarray from the NSDictionary of NSArray using objectForKey
I have a NSArray
which contain n number of NSDictionary
sampleArray = 我有一个
NSArray
包含n个NSDictionary
sampleArray =
(
{
0 = 0;
1 = 0;
},
{
0 = 86400;
1 = 2;
},
{
0 = 172800;
1 = 4;
},
{
0 = 259200;
1 = 5;
}
)
Now I need to fetch the NSArray for objectForKey
0 [sampleArray objectForKey:[NSNumber numberWithInt:0]]
, my result NSArray should be like (0,86400,172800,259200) but I am unable to fetch the result and the app crashes. 现在,我需要获取
objectForKey
0 [sampleArray objectForKey:[NSNumber numberWithInt:0]]
NSArray,我的结果NSArray应该像(0,86400,172800,259200),但是我无法获取结果,应用崩溃。
Normally for NSDictionary, if key value is set using NSString
valueForKey
the above operation is performed successfully but if key value is set using an object like NSNumber objectForKey
I am unable to perform the operation. 通常对于NSDictionary,如果使用
NSString
valueForKey
设置了键值,则可以成功执行上述操作,但是如果使用NSNumber objectForKey
这样的对象设置了键值,则无法执行该操作。
Please help me to get a solution, any suggestion would be appreciated!! 请帮助我获得解决方案,任何建议将不胜感激!
THIS doesnt work - im sorry: I didnt see you didnt have Strings as keys + I didnt know KVC only works with strings 这不起作用-对不起:我没有看到您没有将Strings作为键+我不知道KVC仅适用于字符串
I leave it though 我虽然离开
what you are looking for is 您正在寻找的是
NSArray *zeros = [mainArray valueForKey:@"0"];
it gets "0" from each dict in array 它从数组中的每个字典获取“ 0”
A very straight forward way if your keys are NSNumber
objects: 如果您的键是
NSNumber
对象,这是一种非常简单的方法:
NSMutableArray *result = [[NSMutableArray alloc] init];
for (NSDictionary *d in a) {
if (d[@0]) {
[result addObject:d[@0]];
}
}
What you have do is 你所要做的是
NSMutableArray *arrResult = [[NSMutableArray alloc]init];
for(int i=0;i<[sampleArray count];i++)
{
NSString *strValue = [[sampleArray objectAtIndex:i] valueforkey:@"0"];
[ arrResult addobject:strValue];
}
let me know it is working or not!!!! 让我知道它是否有效!!!!
Happy coding!!!!! 快乐编码!!!
here you can try NSArray *tempArray 在这里你可以尝试NSArray * tempArray
for (NSDictionary *list in tempArray)
{
[firstArray addObject:[list objectForKey:@"0"];
[secondArray addObject:[list objectForKey:@"1"];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.