UIView.subviews和[NSView子视图]之间的行为差​​异 [英] Behavior difference between UIView.subviews and [NSView subviews]

查看:187
本文介绍了UIView.subviews和[NSView子视图]之间的行为差​​异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iPhone应用程序中有一段代码,它从UIView子类中删除所有子视图。它看起来像这样:

  NSArray * subViews = self.subviews; 
for(UIView * aView in subViews){
[aView removeFromSuperview];
}

事实上,我从来没有真正地给它很多想法,直到我在Mac OS X应用程序(从NSView子类)几乎相同的事情:

  NSArray * subViews = [self subviews]; 
for(NSView * aView in subViews){
[aView removeFromSuperview];
}

这完全不起作用。具体来说,在运行时,我得到这个:

  *** Collection< NSCFArray:0x1005208a0>被枚举时发生突变。 

我最后这样做:

  NSArray * subViews = [[self subviews] copy]; 
for(NSView * aView in subViews){
[aView removeFromSuperview];
}
[subViews release];

没问题。但是,什么是Bugging我,是为什么它在iPhone上工作?



子视图是一个复制属性:

  @property readonly,copy)NSArray * subviews; 

我的第一个想法是,当指定copy属性时,可能@ synthesize' getter返回一个副本。 文档清楚了复制的语义,但是似乎没有为getter(或至少,对我不明显)说方式。实际上,做一些我自己的测试,这显然不是这样的。这是好的,我认为返回一个副本将是有问题的,由于几个原因。



所以问题是:上述代码如何在iPhone上工作? NSView显然返回一个指向实际的数组的子视图,也许UIView不是。



任何人都可以提供任何洞察力?


<这是一个在 - [NSView subviews] 中的错误。



幸运的是,有一个更简单的方法来删除所有子视图:

  [self setSubviews:[NSArray array]]; 


I have a piece of code in an iPhone app, which removes all subviews from a UIView subclass. It looks like this:

NSArray* subViews = self.subviews;
for( UIView *aView in subViews ) {
    [aView removeFromSuperview];
}

This works fine. In fact, I never really gave it much thought until I tried nearly the same thing in a Mac OS X app (from an NSView subclass):

NSArray* subViews = [self subviews];
for( NSView *aView in subViews ) {
    [aView removeFromSuperview];
}

That totally doesn’t work. Specifically, at runtime, I get this:

*** Collection <NSCFArray: 0x1005208a0> was mutated while being enumerated.

I ended up doing it like so:

NSArray* subViews = [[self subviews] copy];
for( NSView *aView in subViews ) {
    [aView removeFromSuperview];
}
[subViews release];

That's fine. What’s bugging me, though, is why does it work on the iPhone?

subviews is a copy property:

@property(nonatomic,readonly,copy) NSArray *subviews;

My first thought was, maybe @synthesize’d getters return a copy when the copy attribute is specified. The doc is clear on the semantics of copy for setters, but doesn’t appear to say either way for getters (or at least, it’s not apparent to me). And actually, doing a few tests of my own, this clearly does not seem to be the case. Which is good, I think returning a copy would be problematic, for a few reasons.

So the question is: how does the above code work on the iPhone? NSView is clearly returning a pointer to the actual array of subviews, and perhaps UIView isn’t. Perhaps it’s simply an implementation detail of UIView, and I shouldn’t get worked up about it.

Can anyone offer any insight?

解决方案

This is a bug in -[NSView subviews]. I could have sworn it was fixed for Snow Leopard, but can't find any evidence now.

Fortunately, there's a simpler way to remove all subviews:

[self setSubviews:[NSArray array]];

这篇关于UIView.subviews和[NSView子视图]之间的行为差​​异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆