在NSArray中查找最大宽度的rects

时间:2011-04-14 10:30:59

标签: objective-c cocoa-touch ios nsarray cgrect

我使用的NSArray包含CGRect个值。有没有办法通过库方法获得数组中包含的所有rects的最大宽度,还是我必须实现自己的逻辑?提前谢谢。

代码示例:

for (int i=0; i<[array1 count]; i++) {
    CGRect rect = [[array1 objectAtIndex:i] CGRectValue];
// Here, some transformation of rects

[array2 addObject:[NSValue valueWithCGRect:rect]];

}

在此代码中,我尝试从array1获取帧并将其添加到array2。我从XML获取array1中的帧。我解析了这些帧并将它们放在数组中。我以非常简单的方式提供了我的代码。

2 个答案:

答案 0 :(得分:1)

不要一遍又一遍地向您的阵列发送-count消息。这太浪费了。

float result = 0.0;
for (value in array1)
  {
  GCRect rect = [value CGRectValue];
  result = MAX(result, rect.size.width);
  }  // "result" now contains the greatest width you saw in the loop.

答案 1 :(得分:0)

for(int i=0; i < [array2 count]; i++) 
{
    CGRect rect = [[array2 objectAtIndex:i] CGRectValue];
    float widthTest = rect.size.width;
    //this gives you the width of each element. Compare and get the biggest
}