我使用的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
中的帧。我解析了这些帧并将它们放在数组中。我以非常简单的方式提供了我的代码。
答案 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
}