• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Best Practice for Returning Immutable Objects?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Best Practice for Returning Immutable Objects?


  • Subject: Re: Best Practice for Returning Immutable Objects?
  • From: Karl Moskowski <email@hidden>
  • Date: Tue, 3 Jun 2008 12:20:05 -0400


On 3-Jun-08, at 12:08 PM, Shawn Erickson wrote:

On Tue, Jun 3, 2008 at 8:56 AM, Karl Moskowski
<email@hidden> 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;

Does this generalize to other non-collection classes, e.g., NSString?

It fully depends what the caller expects (aka how you document it). If they expect the data instance they get back to not change on them while they hold a reference (what you imply by saying you return an immutable object) then you need to ensure that holds true either by copying it before you return it or by not modifying it during some well defined (documented) scope/window of time.

For now, at least, I'm the only caller, but that could change in the future.


If you populate the mutable object and never touch it again then you
likely can happily return that instance cast as an NSData since your
API states that the caller should consider it immutable ... however be
aware that the caller may make assumptions about the cost of copying
(no-op for immutable objects) and that assumption could be
invalidated when you return a mutable object instead of the promised
immutable object.


That's what I wanted to know. Thanks.

----
Karl Moskowski <email@hidden>
Voodoo Ergonomics Inc. <http://voodooergonomics.com/>

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

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

References: 
 >Best Practice for Returning Immutable Objects? (From: Karl Moskowski <email@hidden>)
 >Re: Best Practice for Returning Immutable Objects? (From: "Shawn Erickson" <email@hidden>)

  • Prev by Date: Re: Best Practice for Returning Immutable Objects?
  • Next by Date: Re: Main window disappears. Sometimes.
  • Previous by thread: Re: Best Practice for Returning Immutable Objects?
  • Next by thread: Re: Best Practice for Returning Immutable Objects?
  • Index(es):
    • Date
    • Thread