Re: Using NSMutableString* for NSString* result
Re: Using NSMutableString* for NSString* result
- Subject: Re: Using NSMutableString* for NSString* result
- From: Wade Tregaskis <email@hidden>
- Date: Sat, 4 Oct 2003 11:57:17 +1000
In some cases it comes down to your paranoia level versus your desire
for efficiency level. If you just return a mutable string that has
been cast to an NSString, then some _bad_ programmer elsewhere can
ignore the declared type and change the string behind your back,
potentially causing problems for you. Or else that programmer can hold
onto your string - thinking it won't change, and then you change it
behind their back.
Not true. Whenever you wish to retain an NSString you didn't create
(or perhaps even one you did), you should use it's copy method, not
retain. Copy will perform the appropriate action for the real NSString
type (immutable or mutable), whereby immutable strings are simply
retained, and mutable ones have an immutable copy returned.
You only retain an NSString or NSMutableString in very specific cases,
where you explicitly want to see any changes made to it. In such a
case, you'd probably also need a locking mechanism around accesses to
that string, depending on how thread-safe NSString's are.
Note that most interface elements retain the objects you give them,
rather than copy, both for efficiency and so that when you update those
objects, the interface will be updated next time it is
reloaded/refreshed.
Wade Tregaskis
-- Sed quis custodiet ipsos custodes?
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.