Search code examples
objective-ccocoa-touchiosnsarraycgrect

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 very simple way.


Solution

  • 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.