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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:58:27+00:00 2026-05-31T03:58:27+00:00

I have been experimenting a bit with Objective-C and noted some, at least to

  • 0

I have been experimenting a bit with Objective-C and noted some, at least to me, rather strange behavior. First I define a pointer to an NSString and add it to an NSArray:

NSString *s = @"A";
NSArray *a = [NSArray arrayWithOject: s];

I then print out the value of s as well as the contents of a:

NSLog(@"%@", s);
NSLog(@"%@", myArray);

and in both cases the output is A. Now, if I change the pointer s, say

s = @"B";

then the two NSLog statements print out B and A, respectively.

That is, the pointer in my array still points to @"A". After spending years coding in Java, this is very surprising to me. Am I missing something really fundamental here?

Thanks,
Michael Knudsen

  • 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-31T03:58:28+00:00Added an answer on May 31, 2026 at 3:58 am

    The easiest way to understand this is that @"A" creates a new NSString object.

    In your code, you set the pointer s to point to this object, and then add the original object (not the pointer) to an array. You then change the address that the pointer points to to a new address.

    If you want to change the original object, then use NSMutableString and modify the actual object (instead of changing the pointer to a new object) and they will both update as you expect.

    Try:

     NSMutableString *s = @"A";
     // Add to array
     [s setString:@"B"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been experimenting some problems with the fully distributed version. First of all
I have been doing a bit of experimenting, and have discovered that an exception
I've been experimenting quite a bit with my new macbook pro and have run
I have been experimenting a lot with some glassy images, such as the one
I am a bit of a beginner with animations and have been experimenting with
With the release of Gingerbread, I have been experimenting with some of the new
I have been experimenting with WP7 apps today and have hit a bit of
I have been experimenting with woopra.com A web analytics tool. Which requires a piece
I have been experimenting with WPF and rendering strict XAML markup in a web
I have been experimenting with Lambda expressions in Oxygene. Very simple recursive lambda expression

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.