A follow up of sorts to Cocoabuilder - ([EMAIL PROTECTED]) NSDictionary mutability test

from Cocoabuilder - (Bill Bumgarner) Re: NSDictionary mutability test

Performance

An NSMutableDictionary instance can be returned from a method declared
as returning (NSDictionary*) without risk that the client is going to
go and change the contents behind your back.  If "test for mutability"
were common, then all kinds of methods across the 'kits would have to -
copy the return value and, potentially, deeply.

also:Cocoabuilder - (Andy Lee) Re: NSDictionary mutability test

Yup.  I just did a quick test using -[NSView subviews].  The return
type is NSArray*, but if you send -addSubview: to the view, the size
of the previously returned array grows by 1.

I would never assume a returned array is immutable just because the
declared return type of a method is NSArray*.


NSView advertising itself as returning an NSArray* but actually returning a mutable class does have its sneaky consequences.

[[NSView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

this fails with one of dear our old friends: Collection <NSCFArray: 0x128d4e0> was mutated while being enumerated.

we need:

NSArray *subviews = [[NSView subviews] copy];
[subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

Jonathan Mitchell

Central Conscious Unit
http://www.mugginsoft.com




_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to