Re: Best Practice for Returning Immutable Objects?
Re: Best Practice for Returning Immutable Objects?
- Subject: Re: Best Practice for Returning Immutable Objects?
- From: Mike Abdullah <email@hidden>
- Date: Tue, 3 Jun 2008 17:13:22 +0100
On 3 Jun 2008, at 16:56, Karl Moskowski wrote:
I have a few methods that return NSData objects, but the objects are
created and manipulated as NSMutableData, and then copied to an
immutable version along these lines:
NSMutableData *myData = [NSMutableData data];
:
:
return [NSData dataWithData:myData];
Would it be sufficient to cast, like this?
:
return (NSData *)myData;
Well this cast is fairly pointless as it makes no change to the data
at all. Returning an NSMutableData object from an NSData method is
perfectly legitimate as it is a subclass. So you might as well just do:
return myData;
Really, it's up to you. You can just use the simple option of
returning a mutable object, or you can do either of these:
return [NSData dataWithData:myData];
return [[myData copy] autorelease];
Does this generalize to other non-collection classes, e.g., NSString?
TIA.
----
Karl Moskowski <email@hidden>
Voodoo Ergonomics Inc. <http://voodooergonomics.com/>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden