如何在iphone中访问字典值数组

时间:2012-04-09 14:10:33

标签: iphone

我有字典内容数组,但我无法获取字典值。 下面是字典格式,在字典“resdata”中,时间戳是字典键值。所以我需要知道如何获取时间戳值。

 <__NSArrayM 0x89426d0>(
{
    resData =     (
                {

                timestamp = "2012-04-09 13:54:08 +0000";

               }
    );
    seqCounter = 101;
}


here is the source code
 for (int i = 0; i < [self.gluClkDetailArray count]; i++) 
        {
            NSMutableDictionary *mDict = [self.gluClkDetailArray objectAtIndex:i];
            NSDate *mDate = [[mDict objectForKey:@"resData"] objectForKey:@"timestamp"];
            NSLog(@"NSDATE-----%@", mDate);
        }

In above code the dictionary value is 0.

Thanks in advance

1 个答案:

答案 0 :(得分:1)

resData是一系列词典。

索引数组:

for (NSDictionary *mDict in self.gluClkDetailArray) {
    NSArray *resData = [mDict objectForKey:@"resData"];
    NSString *timestamp = [[resData objectAtIndex:0] objectForKey:@"timestamp"];
    NSLog(@"timestamp: %@", timestamp);
}

示例(使用llvm 4.0):

NSArray *a = @[ @{ @"resData"    : @[ @{ @"timestamp" : @"2012-04-09 13:54:08 +0000" } ],
                   @"seqCounter" : @101
                 }
             ];
NSLog(@"a: %@", a);

for (NSDictionary *mDict in a) {
    NSArray *resData = [mDict objectForKey:@"resData"];
    NSString *timestamp = [[resData objectAtIndex:0] objectForKey:@"timestamp"];
    NSLog(@"timestamp: %@", timestamp);
}

NSLog输出:

a: (
        {
        resData =         (
                        {
                timestamp = "2012-04-09 13:54:08 +0000";
            }
        );
        seqCounter = 101;
    }
)
  

时间戳:2012-04-09 13:54:08 +0000