[英]Finding largest width of rects in NSArray
I am using an NSArray
which contains CGRect
values. 我正在使用包含CGRect
值的NSArray
。 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? 有什么方法可以通过库方法来获得数组中包含的所有rect的最大宽度,还是我必须实现自己的逻辑? 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
. 在这段代码中,我试图从array1
获取帧并将其添加到array2
。 I am getting the frames in array1
from XML. 我从XML获取array1
的帧。 I parsed those frames and placed them in the array. 我解析了这些帧并将它们放置在数组中。 I provided my code as a very simple way. 我以非常简单的方式提供了代码。
Don't send a -count
message to your array over and over like that. 请勿一遍又一遍地向您的阵列发送-count
消息。 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.