如何访问所有字典内容数组

时间:2012-04-13 07:01:37

标签: iphone

我已经实现了字典内容访问方法的数组,但它只访问一个内容,请帮助解决这个问题

这是我的数据文件

<__NSArrayM 0x8b7a350>(
{
    clkDate = "Wednesday, April 11, 2012";
    resData =     (
                {
            data = 10;
            independentItem = 0;
            module = 6;
            newDate = "11-4-2012";
            newTime = "13:31";
            seqCounter = 101;
            sequence = 10007;
            session = 101;
            timeframe = Breakfast;
            timestamp = "2012-04-11 08:01:27 +0000";
            title = "Glucose reading : Low";
            type = L;
        },
                {
            data =             {
                hours = 0;
                minutes = 0;
                which = "Walking:(null)";
            };
            independentItem = 1;
            module = 13;
            seqCounter = 101;
            sequence = 10009;
            session = 101;
            timestamp = "2012-04-12 08:01:40 +0000";
            title = "Low Cause: Increased Exercise";
        }
    );
    seqCounter = 101;
},
{
    clkDate = "Thursday, April 12, 2012";
    resData =     (
                {
            data = 200;
            independentItem = 0;
            module = 6;
            newDate = "12-4-2012";
            newTime = "13:31";
            seqCounter = 102;
            sequence = 10017;
            session = 101;
            timeframe = Breakfast;
            timestamp = "2012-04-12 08:01:46 +0000";
            title = "Glucose reading : High";
            type = H;
        },
                {
            data =             {
                hours = 0;
                minutes = 0;
                which = "Other Light:Kkkkk";
            };
            independentItem = 1;
            module = 26;
            seqCounter = 102;
            sequence = 10022;
            session = 101;
            timestamp = "2012-04-11 18:30:00 +0000";
            title = "High Cause: Decreased Exercise";
        }
    );
    seqCounter = 102;
},
{
    clkDate = "Thursday, April 12, 2012";
    clkIndependentItem = 1;
    resData =     (
                {
            data =             {
                bathing = 1;
                driving = 1;
                grocery = 1;
                meal = 1;
            };
            independentItem = 1;
            module = 31;
            seqCounter = 103;
            sequence = 10035;
            session = 101;
            timestamp = "2012-04-12 08:02:32 +0000";
            title = "High Cause: Decreased activity";
        }
    );
    seqCounter = 103;
}
)

在上面的数据文件中,我需要访问“independentItem = 1”相关项

我试过源代码是

for (int i = 0; i < [self.gluClkDetailArray count]; i++) 
        {
            NSMutableDictionary *mDict = [self.gluClkDetailArray objectAtIndex:i];
            {
                NSMutableArray *mResData = [mDict objectForKey:@"resData"];
                NSDate *mTimestamp = [[mResData objectAtIndex:0] objectForKey:@"timestamp"];

                NSDateFormatter *mFormatter = [[NSDateFormatter alloc] init];
                [mFormatter setDateStyle:NSDateFormatterFullStyle];

                NSString *mClkDate = [mFormatter stringFromDate:mTimestamp];
                [mDict setValue:mClkDate forKey:@"clkDate"];

                NSString *mIndItem = [[mResData objectAtIndex:0] objectForKey:@"independentItem"];
                if (mIndItem.intValue == 1) 
                {
                    [mDict setValue:mIndItem forKey:@"clkIndependentItem"];
                    [mIndpendentArray addObject:mDict];
                }
                [gluClkResultArray addObject:mDict]; 

        }

当我把mResData objectAtIndex:0而不是i时,应用程序崩溃了。我已根据序列计数器保存了字典值数组

提前致谢

1 个答案:

答案 0 :(得分:0)

更改

NSString *mIndItem = [[mResData objectAtIndex:0] objectForKey:@"independentItem"];

到那个:

NSString *mIndItem = [[mResData objectAtIndex:1] objectForKey:@"independentItem"];

independentItem位于索引1处的字典中,而不是0。

希望有所帮助