开发者

Finding largest width of rects in NSArray

I am using an NSArray which contains CGRect values. Is there any way to get the greatest width of all the rects contained in the array through library methods, or do I have to implement my own logic? Thanks in advance.

Code sample:

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

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

}

In this code I am trying to take frames from array1 and add them to array2. I am getting the frames in array1 from XML. I parsed those frames and placed them in the array. I provided my code as a ver开发者_JAVA技巧y simple way.


Don't send a -count message to your array over and over like that. It's wasteful.

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.


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜