Iterate through custom objects in a UIView and fin

2019-08-19 13:36发布

I have a UIView that gets loaded with several custom NSObjects (graphShape). Each graphShape object has a property called shapeName. I can access this property like this:

graphShape * myNewShape = [doShapes objectAtIndex:i];
NSLog(@"myNewShape Name: %@", myNewShape.shapeName);
//Logs: myNewShape Name: redshape 

The NSArray (doShapes) contains a list of some shapeNames that the View contains (ie: 'redshape', and 'yellowshape') and I need to change another property of the matched object How do I loop through all of the instances of the graphShape object in my view and find the ones that have the property 'redshape' and 'yellowshape'?

2条回答
来,给爷笑一个
2楼-- · 2019-08-19 13:44

Are you looking for that:

for (graphShape * shape in doShapes){
  if ( [shape.shapeName isEqualToString:@"redshape"] ){
    //do stuff
  }
}
查看更多
甜甜的少女心
3楼-- · 2019-08-19 14:02

Another approach to obtain a subset of objects that match your criteria using blocks:

NSSet *setOfRedViewShapes = [doShapes objectsPassingTest:^(id obj, BOOL *stop){

 return [obj isEqualToString@"redShape"];

}];

You can then iterate over the objects in setOfRedViews...

查看更多
登录 后发表回答