UIView ivar 与标记本地 UIView var
场景一: 对于 UIViewController,最好 (1) 为我在 loadView
之外的 1 或 2 个函数中再次访问的 UIView 创建一个 ivar?或者,(2)我应该在 loadView
中标记它,然后使用 - (UIView *)viewWithTag:(NSInteger)tag
在其他函数中再次访问它?我猜测选项 1 将内存增加了指针的大小,即 32/64 位,并创建访问器方法(假设我声明 @property
& @synthesize),然后需要在dealloc中释放ivar并在viewDidUnload中将其设置为nil...并且选项2可以节省内存,设置代码较少,但需要一些处理时间和一些额外的代码来通过标签查找视图。我对这一切的看法正确吗?
在这种情况下,感觉最好使用 ivar,但我不确定。
场景2: 对于具有 5 个子视图的 UIView 自定义子类怎么样?请记住,在给定时间,我将在内存中拥有大约 30 个此自定义子类的实例(它们将是 tableViewCell
的子视图。),我应该为子视图使用 5 个 ivars,还是我应该给它们全部贴上标签吗?
在这种情况下,我认为通过标记它们节省的内存值得使用 - (UIView *)viewWithTag:(NSInteger)tag
搜索它们带来的小性能损失。
想法?
谢谢!
马特
Scenario 1:
For a UIViewController, is it better to (1) create an ivar for a UIView that I access again in 1 or 2 functions outside of loadView
? Or, (2) should I just tag it in loadView
and then use - (UIView *)viewWithTag:(NSInteger)tag
to access it again in the other functions? I'm guessing that option 1 increases the memory by the size of a pointer, so 32/64-bits, and creates accessor methods (assuming I declare @property
& @synthesize
), and then requires releasing the ivar in dealloc
and setting it to nil
in viewDidUnload
... and that option 2 saves memory, has less setup code, but costs some processing time and a little extra code to find the view by its tag. Am I right about all this?
In this scenario, it feels best to use an ivar, but I'm not sure.
Scenario 2:
What about for a custom subclass of UIView that has 5 subviews? Bearing in mind that I'll have about 30 instances of this custom subclass in memory at a given time (They'll be subviews of tableViewCell
s.), should I use 5 ivars for the subviews, or should I tag them all?
In this scenario, I'm thinking the memory saved by tagging them all would be worth the small performance hit of searching for them with - (UIView *)viewWithTag:(NSInteger)tag
.
Thoughts?
Thanks!
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
考虑到内存使用量的差异可以忽略不计(除非您将拥有 100 个这样的视图,在这种情况下您可能想研究如何重用其中一些视图),您可能应该考虑什么会让您的代码更有用可读且可维护。我个人认为使用 ivar 会更具可读性,但对于您的特定情况,使用标签也可能更具可读性。
在编写代码时,我总是尝试考虑一两年后将阅读代码的人。这个人可能是我,也可能是其他人,但无论哪种方式,我知道那个人都会欣赏可读的代码。他们不太可能感谢我在至少有 128 MB RAM 的设备上节省了 1k 内存。
Considering that difference in memory usage is negligible (unless you're going to have 100 of these views, in which case you might want to look into how you can reuse some of those views), you should probably consider what would make your code more readable and maintainable. I personally think that using an ivar would be more readable, but it's also possible that for your particular case using tags would be more readable.
When writing code, I always try to think about the person who's going to reading the code a year or two years from now. That person might be me, or it might be someone else, but either way, I know that person will appreciate readable code. They're less likely to thank me for saving 1k of memory on a device that has at least 128 MB of RAM.
考虑一下,每个视图都由 CALayer 支持。 IIRC,视图大约为 44 字节(加上 foo),图层大约为 44 字节(乘以 3,因为有表示树和渲染树),并且执行任何渲染的图层均由位图上下文支持。
或者,进行更直接的比较:每个指针消耗的内存与单个像素一样多。
我只在标签让我的生活更轻松的地方使用标签:
layer.style
...)。这通常与前两个地方相关。另外,请记住
[v viewWithTag:tag]
可以返回v
、任何子视图、任何子视图的子视图...考虑一个具有“内容视图”的 FooView 类” 带有标签 1,“工具栏视图” 带有标签 2:它打印什么?好吧,它们都可以成为
f2
的工具栏!是的,苹果可以使搜索更加合理(它可以继续搜索,直到找到最不深度的匹配,或者使用迭代加深深度优先搜索),但我假设它执行简单的深度优先搜索,除非文档另有说法。
Consider, for a moment, that each view is backed by a CALayer. IIRC, views are about 44 bytes (plus foo), layers are about 44 bytes (times 3, since there's a presentation tree and render tree), and layers which do any rendering are backed by bitmap contexts.
Or, for a more straightforward comparison: Each pointer consumes as much memory as a single pixel.
I use tags only where they make my life easier:
layer.style
...). This is generally relevant to the first two places.Also, remember that
[v viewWithTag:tag]
can returnv
, any subview, any subview-of-subview... Consider a class FooView which has a "content view" with tag 1, and a "toolbar view" with tag 2:What does it print? Well, they both could be
f2
's toolbar!Yes, Apple could make the search more sensible (it could continue the search until it found the least-deep match, or use iterative-deepening depth first search), but I'd assume that it performed simple depth-first search unless the documentation says otherwise.
我认为你问错了问题。
在我看来,存储 5 个指针所需的内存或使用 viewWithTag: 查找 5 个视图所需的时间在总体方案中都可以忽略不计。
选择您发现的使您的代码最容易编写、理解和维护的解决方案。
万一时间/内存使用情况的实际分析表明您选择的解决方案存在问题,请考虑其他选项。其他任何事情都是过早的优化。
I think you're asking the wrong question.
It seems to me that either the memory required to store 5 pointers, or the time required to look up 5 views using
viewWithTag:
is going to be negligible in the grand scheme of things.Choose whichever solution you find makes your code easiest to write, understand, and maintain.
In the unlikely event that actual profiling of time/memory usage indicates that your chosen solution is a problem, consider the other option. Anything else is premature optimization.