gpt4 book ai didi

ios - 通过 subview 或 NSMutableArray 枚举更快吗?

转载 作者:行者123 更新时间:2023-12-01 19:12:43 25 4
gpt4 key购买 nike

我真的在尝试优化我的移动代码 UIImageViews .

我有一个主要的方法来枚举我所有的 UIImageViews .

哪个枚举执行得更快,或者它们的执行完全相同?

示例 1:在此示例中,我枚举了所有 UIImageViews在我的UIViewController的观点

 for (UIImageView *imageView in self.view.subviews)
{
...
}

示例 2:在此示例中,我枚举了所有 UIImageViews在我的 NSMutableArray .该数组始终包含所有 UIImageViews发现于 self.view.subviews (它只是通常在 self.view.subviews 中找到的所有 UIImageViews 的副本,我只需要创建一次)
 for (UIImageView *imageView in self.imageViews)
{
...
}

那么哪个示例应该为我提供更快、更优化的枚举呢?请注意,这些是由我设置的 CADisplayLink 不断枚举的。另请注意 self.view.subviews返回 NSArray所以也许这 NSArray当我使用第一个枚举时不断创建,让我认为 NSMutableArray方法会表现更好并且开销更少,但我不确定。

编辑:所有 subview 都是 UIImageViews .我也不确定在使用 subviews 属性时是否有任何重大开销,因为它看起来可能会做一些“幕后”工作来让我使用 NSArray。

最佳答案

您可以使用 Tag,而不是遍历 subview 。属性做的非常快

例子:

添加标签到 UIimageView通过code or xib

myImageView.tag = 1;

然后,
 UIImageView *myImageView = ([[self.view viewWithTag:1] isKindOfClass:[UIImageView class]])?(UIImageView *)[self.view viewWithTag:1]:nil;

关于您的迭代代码

假设所有 subview 都是 UIImageViews 那么第一个代码将是最有效的。
for (UIImageView *imageView in self.view.subviews)

关于ios - 通过 subview 或 NSMutableArray 枚举更快吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15376492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com