objective-c - 查找 NSArray 中矩形的最大宽度

标签 objective-c cocoa-touch ios nsarray cgrect

我正在使用包含 CGRect 值的 NSArray 。有没有办法通过库方法获得数组中包含的所有矩形的最大宽度,或者我是否必须实现自己的逻辑?提前致谢。

代码示例:

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

最佳答案

不要像这样一遍又一遍地向您的数组发送 -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.

关于objective-c - 查找 NSArray 中矩形的最大宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5661880/

相关文章:

ios - UISlider 移动不稳定和/或重绘缓慢

ios - 存折生成 PKPass 错误

iphone - 如何在 iPhone 的表格 View 单元格中设置背景图像

ios - XCode 8 编译真的很慢,SourceKitService 使用 300+ % CPU

ios - 如何在 iOS App 中解析 JSON

ios - AFNetworking 请求是否应该放在与 View Controller 分开的 NSObject 中?

iphone - 使用 UIAccleration 获得旋转级别

iphone - 让 iPhone 振动

ios - iOS 7中的Spotlight TableView是怎么做出来的?

ios - 修改后的 UISegmentedControl 的应用商店批准