如何迭代字典?

时间:2013-08-13 06:28:41

标签: ios iteration nsmutabledictionary

我有一本字典,在这本字典中,我有数组。在这些数组中,我有包含子视图的视图。其中一个子视图是uilabel,我需要达到那个uilabel。

当我尝试迭代它时,我会收到错误“集合表达式类型uiview可能无法响应countbyenumaretingwithstate:objects:count”

我的代码如下;

for(int i = 0; i<counterRow;i++)
{
    NSString *dictTempKey = [NSString stringWithFormat:@"row%d",i];

  NSMutableArray * tempArray =  [doctorAddition objectForKey:dictTempKey];

    for(UIView * subview in tempArray)
    {

        UIView * temp = subview;

        for(UIView * subview2 in (UIView*)temp)
        {
            if([subview2 isKindOfClass:[UILabel class]])
            {

            }

        }

    }

}

3 个答案:

答案 0 :(得分:3)

我认为你错过了这个(UIView*)temp.subviews,第三个是:

for(int i = 0; i<counterRow;i++)
{
    NSString *dictTempKey = [NSString stringWithFormat:@"row%d",i];

  NSMutableArray * tempArray =  [doctorAddition objectForKey:dictTempKey];

    for(UIView * subview in tempArray)
    {

        UIView * temp = subview;

        for(UIView * subview2 in (UIView*)temp.subviews)
        {
            if([subview2 isKindOfClass:[UILabel class]])
            {

            }

        }

    }

}

答案 1 :(得分:1)

您可以执行以下操作....

NSArray *values = [doctorAddition allValues];

[values enumerateObjectsUsingBlock:^(UIView *aView, NSUInteger index, BOOL *stop) {

    [aView.subviews enumerateObjectsUsingBlock:^(id aSubView, NSUInteger index, BOOL *stop) {

        if ([aSubView isKindOfClass:[UILabel class]]) {
            NSLog(@"Found UILabel: %@", aSubView);
        }
    }];
}];

答案 2 :(得分:0)

//如果一个视图有更多的子视图(即UILable,imageview,UIButton..etc),现在如何识别一个特定的视图..下面代码是最好的例子..

//这是一个通用示例,这将有助于所有用户

for (UIView *subview_obj in [View_obj subviews])// Here it ll fetch all subview from view
{
   if ([subview_obj isKindOfClass:[UIButton class]])// here it ll identify perticular view from all subview
   {
     // Do Something
   }
}