• 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: Using NSMutableString* for NSString* result
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using NSMutableString* for NSString* result


  • Subject: Re: Using NSMutableString* for NSString* result
  • From: Greg Titus <email@hidden>
  • Date: Fri, 3 Oct 2003 09:55:41 -0700

On Friday, October 3, 2003, at 08:28 AM, Matt Gough wrote:

Is it OK to just return (NSString*)myMutableString, or do I have to do copy
the string in to a 'proper' NSString? (It is assumed that the function has
no knowledge as to how its result will be used elsewhere)

As you can see from your two responses so far, there isn't general agreement on this topic. :-)

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.

For one example, Apple has chosen efficiency when it comes to the NSTextView -string method. It's declared as NSString but it's actually the internal mutable text and it'll change if the text view changes, so you need to remember to -copy the result if you actually need a fixed string that won't change on you. (This gets my vote for how you should implement things, by the way.)

But!

For your specific example code: _always_ just return (NSString *)myMutableString. You are not holding onto the mutable string yourself - it is just a temporary object as far as you are concerned. So you'll never care if some other programmer cheats and modifies it later, and neither will that other programmer have to worry about it changing out from under them. So there is absolutely no reason to create an additional immutable string object.

Hope this helps,
- Greg
_______________________________________________
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.

  • Follow-Ups:
    • Re: Using NSMutableString* for NSString* result
      • From: Wade Tregaskis <email@hidden>
References: 
 >Using NSMutableString* for NSString* result (From: Matt Gough <email@hidden>)

  • Prev by Date: Re: Using NSMutableString* for NSString* result
  • Next by Date: Re: Using NSMutableString* for NSString* result
  • Previous by thread: Re: Using NSMutableString* for NSString* result
  • Next by thread: Re: Using NSMutableString* for NSString* result
  • Index(es):
    • Date
    • Thread