Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 828717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:46:05+00:00 2026-05-15T03:46:05+00:00

Ok, so I have a method that takes an NSString as input, does an

  • 0

Ok, so I have a method that takes an NSString as input, does an operation on the contents of this string, and returns the processed string. So the declaration is:

- (NSString *) processString: (NSString *) str;

The question: should I just return the NSMutableString instance that I used as my “work” buffer, or should I create a new NSString around the mutable one, and return that?

So should I do this:

- (NSString *) processString: (NSString *) str
{
    NSMutableString *work = [NSMutableString stringWithString: str];
    // process 'work'
    return work;
}

Or this:

- (NSString *) processString: (NSString *) str
{
    NSMutableString *work = [NSMutableString stringWithString: str];
    // process 'work'
    return [NSString stringWithString: work]; // or [work stringValue]?
}

The second one makes another copy of the string I’m returning, unless NSString does smart things like copy-on-modify. But the first one is returning something the caller could, in theory, go and modify later. I don’t care if they do that, since the string is theirs. But are there valid reasons for preferring the latter form over the former? And, is either stringWithString or stringValue preferred over the other?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:46:06+00:00Added an answer on May 15, 2026 at 3:46 am

    Depends entirely on how defensive you need to be.

    There is no reasonable way a client of your code can test for mutability. Thus, there is no reasonable way that a client can cast from NSString to NSMutableString and suddenly achieve no-compiler-warning mutability of the string.

    So, as long as you trust your clients, you might as well return the mutable string (as an NSString). Even if the client does something stupid and mutates it, compiler warnings be damned, nothing breaks.

    Where it does matter is when you are returning the mutable backing store of some object that should not be mutated externally. In that case, the client might ignore the compiler warnings and/or cast to the mutable variant and completely bork your object.

    Or, more subtly, the client might grab a reference to the supposedly immutable return type and hold onto it long enough that it is mutated later, leaving their reference in a surprising state.

    Best practice: When computing and returning a transient value, mutability is irrelevant. When returning a reference to an internal mutable store, best to make an immutable copy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 515k
  • Answers 515k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This was because I didn't have the Mobile Device Center… May 16, 2026 at 6:36 pm
  • Editorial Team
    Editorial Team added an answer EDIT: The Yahoo Where API is no longer available. Try… May 16, 2026 at 6:36 pm
  • Editorial Team
    Editorial Team added an answer Generally an operator of that form would be declared as:… May 16, 2026 at 6:36 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a WCF service, in it i have a method that takes a
Hey guys. I have a method that gets called each second which I want
I have this method and it's kind of really big so I can't include
I am making an app that takes photos from web site for some Username
So basically I have a big list of buttons that's present dropdowns and other
I have an app that allows you to take a picture, adjust the jpeg
Suppose (for the sake of argument) that I have a view class which contains
I am building a C# Desktop application. How do I call a method that
I am changing the image in UIImageView based on accelerometer input. The images are
I have a simple UIView *myView which is set as follows myView.image = [UIImage

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.