title: Behavior difference between UIView.subviews and [NSView subviews]
body: <p>I have a piece of code in an iPhone app, which removes all subviews from a UIView subclass.  It looks like this:</p>
  
  <pre><code>NSArray* subViews = self.subviews;
  for( UIView *aView in subViews ) {
      [aView removeFromSuperview];
  }
  </code></pre>
  
  <p>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):</p>
  
  <pre><code>NSArray* subViews = [self subviews];
  for( NSView *aView in subViews ) {
      [aView removeFromSuperview];
  }
  </code></pre>
  
  <p>That totally doesn’t work.  Specifically, at runtime, I get this:</p>
  
  <pre><code>*** Collection &lt;NSCFArray: 0x1005208a0&gt; was mutated while being enumerated.
  </code></pre>
  
  <p>I ended up doing it like so: </p>
  
  <pre><code>NSArray* subViews = [[self subviews] copy];
  for( NSView *aView in subViews ) {
      [aView removeFromSuperview];
  }
  [subViews release];
  </code></pre>
  
  <p>That's fine.  What’s bugging me, though, is why does it work on the iPhone?  </p>
  
  <p>subviews is a copy property:</p>
  
  <pre><code>@property(nonatomic,readonly,copy) NSArray *subviews;
  </code></pre>
  
  <p>My first thought was, maybe @synthesize’d getters return a copy when the copy attribute is specified.  <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProperties.html" rel="noreferrer">The doc</a> 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.</p>
  
  <p>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.</p>
  
  <p>Can anyone offer any insight?</p>