Skip navigation.
 
mlRe: subviews question
FROM : Michael Ash
DATE : Thu Aug 31 13:19:38 2006

On 8/30/06, Mu Lin <<email_removed>> wrote:
> Hi, All:
>
> I have a subclass of NSView say MyView only overide the drawRect
> method,  when I obtain subviews using following lines in my
> appController:
>
> MyView *aView; // outlet from nib
> NSArray *mySubViews = [aView subViews];
>
> Now the array mySubViews has some interesting properties, if a
> subview is added or removed from aView, mySubViews updates
> automatically, this is rather a surprise to me.
> Seems to me  - (NSArray *)subviews returns a reference of the
> internal data  representation or it is a singleton. The docs doesn't
> mention anything about this. Any comments?


If the documentation doesn't say, then there are no guarantees. It
would be perfectly allowable for it to return a copy of its internal
array, or for it to simply return a reference to it. You must write
your code to deal with both cases. This isn't very hard; if you need
the array not to change, copy it, and if you need it to change, ask
the view for its subviews each time.

This isn't the only place you will see this. I know that NSTextView's
-string method does something similar, and it occurs in many other
places as well. Code according to what the docs promise, not according
to how you think things should act, and you'll be fine.

Mike

Related mailsAuthorDate
mlsubviews question Mu Lin Aug 31, 01:31
mlRe: subviews question Ben Lachman Aug 31, 06:49
mlRe: subviews question Michael Ash Aug 31, 13:19