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 8086479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:27:42+00:00 2026-06-05T18:27:42+00:00

i found this code -(NSString *) genRandStringLength: (int) len { NSMutableString *randomString = [NSMutableString

  • 0

i found this code

-(NSString *) genRandStringLength: (int) len {

    NSMutableString *randomString = [NSMutableString stringWithCapacity: len];

    for (int i=0; i<len; i++) {
         [randomString appendFormat: @"%C", [letters characterAtIndex: arc4random() % [letters length]]];
    }

    return randomString;
}

is it ok to pass a mutable object, where the result is immutable?

so should we do?

return [randomString copy];

or

return randomString;
  • 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-06-05T18:27:44+00:00Added an answer on June 5, 2026 at 6:27 pm

    Technically, it is no problem to return an NSMutableString instead of an NSString. Since the former is a subclass of the latter, an NSMutableString instance supports everything an NSString does. If you look at Cocoa, you’ll notice, for example, that many alloc/init methods return an instance of a subclass of the class you wanted to create.

    In your particular case, I see no problem if the method returns a mutable string because the string has been created locally in that method and is not being used in other places in the class.

    Generally, you should probably avoid returning mutable objects if these objects are ivars/properties of the class that returns them. If you do such a thing, the caller of the method could find out that the returned object is mutable and then change its contents without notifying the “owning” instance of the object about the changes.

    Also note that, if you want to return an immutable object, return [randomString copy]; is only correct under ARC and garbage collection. With manual memory management, it should be return [[randomString copy] autorelease]; or return [NSString stringWithString:randomString];.

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

Sidebar

Related Questions

I found this code using Google. private int RandomNumber(int min, int max) { Random
I found this code for alphanumeric check (Letters, numbers, spaces or underscores) but I
I've found this code, here : if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { str = [NSString
I found this code, it supposed to find the first cell in the visible
I found this code online that has a procedure inside a procedure. I cannot
I found this code online: def merge(left, right): result = [] i ,j =
I found this code in one of the other questions on here. It's exactly
I found this code to launch the browser with an intent: Intent intent =
I found this code and have been using this to automatically close the window...It
i found this code: protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); HwndSource hwndSource =

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.