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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:51:06+00:00 2026-06-08T03:51:06+00:00

I did find some discussion on this topic, but it was beyond my current

  • 0

I did find some discussion on this topic, but it was beyond my current level of understanding. I’m reading through Kochan’s “Programming in Objective-C, 4e” and am on the section about NSString objects. First, the code I’m playing with so I can ask my question:

NSString *strA = @"StringA";
NSString *strB = @"StringB";
NSLog(@"Value of strA: %@",strA);
NSLog(@"Value of strB: %@",strB);

strB = strA; // strB should point to strA's memory location
NSLog(@"New value of strB: %@",strB); //correctly logs as "StringA"
strA = @"StringA has been changed.";
NSLog(@"New value of strB: %@",strB); //logs as "StringA" still
NSLog(@"New value of strA: %@",strA); //correctly logs as "StringA has been changed"

My understanding is that saying strB = strA would mean that strB is pointing to strA’s memory location, so any changes to strA would also go to strB. However, this doesn’t happen. Kochan says it’s supposed to, and to get around it suggests using

strB = [NSString stringWithString: strA];

From what I can see, either method works. I can say strB = strA, then change strA, yet strB still retains the value it received (the value of strA before just changing it).

I thought this might come from NSString being immutable, but if that’s the case, why can I in fact change the values of str1 and str2 after initialization? What am I missing here? Thanks.

  • 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-08T03:51:08+00:00Added an answer on June 8, 2026 at 3:51 am

    As if you didn’t have enough answers to this already…

    I think a diagram or three will help you understand what’s going on in your code example.

    First, your code does this:

    NSString *strA = @"StringA";
    NSString *strB = @"StringB";
    

    This makes your app’s memory look like this:

    memory diagram 1

    On the left are your local variables, strA and strB. They are just pointers to objects in memory.

    In the middle are the string objects. These are pre-created in your app by the compiler because they appeared in your source code.

    On the right are the actual characters of the string objects. These are also pre-created in your app by the compiler.

    Next, your code does this:

    strB = strA;
    

    Now your app’s memory looks like this:

    memory diagram 2

    Notice that your local variable, strB, changed, but the objects and the characters didn’t change.

    Finally, your code does this:

    strA = @"StringA has been changed.";
    

    This makes your app’s memory look like this:

    memory diagram 3

    Your local variable, strA, has changed, but the objects and the characters still didn’t change.

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

Sidebar

Related Questions

I did find some suggestions for this topic but still there are some things
I found some threads saying this was doable, but did not find specific instructions
I did do some googling and searching on this site but did not find
Did some googling and couldn't find a clear answer on this. My assumption is
I did some research but all I could find was syncing data core with
I did some research but couldn't find the answer I was looking for so
I did some research but cannot find a definite approval or disapproval. What I
Ok, so I did find some questions that were almost similar but they actually
How to install Ldap on windows7 and to configure it?? Did some googling..cant find
I tried to customize django's admin css....but I'm stuck ....i did find a lot

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.