• 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: j o a r <email@hidden>
  • Date: Tue, 3 Jun 2008 09:15:19 -0700


On Jun 3, 2008, at 8:56 AM, Karl Moskowski wrote:

Would it be sufficient to cast, like this?
	:
	return (NSData *)myData;

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


In the general case yes. I would say that it's not up to you to ensure that this object is not being accidently mutated at some later point - you already declared the returned object to be of an immutable type.

If someone later needs to ensure that they hold an immutable and private copy, they can always copy it at that point. If the object being copied is already immutable, this is typically cheap. See the "Copying Mutable Versus Immutable Objects" optimization here:

<http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/ImplementCopy.html#//apple_ref/doc/uid/20000049-999791 >

j o a r


_______________________________________________

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


  • Follow-Ups:
    • Re: Best Practice for Returning Immutable Objects?
      • From: Seth Willits <email@hidden>
References: 
 >Best Practice for Returning Immutable Objects? (From: Karl Moskowski <email@hidden>)

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