• 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: Seth Willits <email@hidden>
  • Date: Tue, 3 Jun 2008 12:07:09 -0700

On Jun 3, 2008, at 9:15 AM, j o a r 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 used to think that. Now I'm converting to making sure it truly is returning an immutable object. There have been cases where I've changed it under myself and run into problems. I figure if it says it's immutable, it'd better be immutable.


That said, because of my own bugs, I've also taken to copying some objects (if given as a parameter to a setObject: method, for instance) instead of simply retaining them. So I guess I'm moving towards being doubly safe.

If I get an object from AppKit or Foundation etc that's an immutable object, I assume it to truly be immutable. Maybe someone should write an automated tool like Ken Ferry did with alloc/init and figure out how many framework calls actually return mutable objects posing as immutable objects? :-)


-- Seth Willits




_______________________________________________

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: "Michael Ash" <email@hidden>
    • Re: Best Practice for Returning Immutable Objects?
      • From: "Kyle Sluder" <email@hidden>
References: 
 >Best Practice for Returning Immutable Objects? (From: Karl Moskowski <email@hidden>)
 >Re: Best Practice for Returning Immutable Objects? (From: j o a r <email@hidden>)

  • Prev by Date: Re: Core Data "entity required" error
  • Next by Date: Re: how to run my app in privileged mode
  • Previous by thread: Re: Best Practice for Returning Immutable Objects?
  • Next by thread: Re: Best Practice for Returning Immutable Objects?
  • Index(es):
    • Date
    • Thread