Domanda

I have one Mutable Array with 20 objects.Each object have the given format

{

 "NONE-NONE-ABCD-RECD" =     {
  Count =         
        {
              bird = "10";
              animal = "4";
        };

  Type = "NONE-NONE-ABCD-RECD";
};

} 

The key NONE-NONE-ABCD-RECD will be different for different objects.Some times become NONE-NONE-NONE-NONE or ABCD-PQWE-MNBW-POWE,etc. I need the bird found and animal found of the each objects.How can i get the counts when keys changes.

È stato utile?

Soluzione

Try this,

NSArray *allKeys = [elementDict allKeys]; //elementDict array object
if ([allKeys count] > 0) {
    NSDictionary *objectDict = [elementDict objectForKey:[allKeys objectAtIndex:0]];
    NSDictionary *countDict = [objectDict objectForKey:@"Count"];
    //from countDict you can access values
    NSString *bird = [countDict valueForKey:@"bird"];
    NSString *animal = [countDict valueForKey:@"animal"];
}

Altri suggerimenti

Well, if Type will everytime returns your the key, you can do this:

NSString *yourKey = [yourDictionary objectForKey:@"Type"];
NSDictionary *countDict = [yourDictionary objectForKey:yourKey];
int bird = [[countDict objectForKey:@"bird"] intValue];
int animal = [[countDict objectForKey:@"animal"] intValue];

Other solution:

for (NSString *aKey in yourDictionary)
{
    if ([[yourDictionary objectForKey:@"aKey"] isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *countDict = [yourDictionary objectForKey:aKey];
        int bird = [[countDict objectForKey:@"bird"] intValue];
        int animal = [[countDict objectForKey:@"animal"] intValue];
    }
}

Other solution, according if your object may contain other sub-dictionary:

for (NSString *aKey in yourDictionary)
{
    if ([[yourDictionary objectForKey:@"aKey"] objectForKey:@"Count"])
    {
        NSDictionary *countDict = [[yourDictionary objectForKey:@"aKey"] objectForKey:@"Count"];
        int bird = [[countDict objectForKey:@"bird"] intValue];
        int animal = [[countDict objectForKey:@"animal"] intValue];
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top