3

I have a simple question. If I am declaring NSString (ref type) as shown below:

 NSString *johnsMoney = @"200";
    NSString *marysMoney = johnsMoney;

    NSLog(@"Johns Money %@",johnsMoney);
    NSLog(@"Marys Money %@",marysMoney);

    johnsMoney = @"100";
    NSLog(@"Johns Money %@",johnsMoney);
    NSLog(@"Marys Money %@",marysMoney);

The output produced is this:

 Johns Money 200
 Marys Money 200
 Johns Money 100
 Marys Money 200

From my understanding when I assign @"100" to johnsMoney should it not also change the value of "marysMoney" to 100 since marysMoney is pointing to johnsMoney.

UPDATE:

I believe the following example shows what I initially was trying to do:

Dog *dog1 = [[Dog alloc] init];
dog1.name = @"Dog 1";

Dog *dog2 = dog1;

NSLog(@"Dog 1 %@",dog1.name);
NSLog(@"Dog 2 %@",dog2.name);

dog1.name = @"Dog 3";

NSLog(@"Dog 1 %@",dog1.name);
NSLog(@"Dog 2 %@",dog2.name);
john doe
  • 9,220
  • 23
  • 91
  • 167

8 Answers8

4

johnsMoney and marysMoney are both pointers to strings.

When you write johnsMoney = @"100", it now points to a different string. This doesn't change marysMoney which still points to the original string.

If you were using NSMutableString, and you did [johnsMoney setString:@"100"], then it would change the underlying data (to which both variables would still be pointing).

jtbandes
  • 115,675
  • 35
  • 233
  • 266
1

They are not references. They are object pointers. If two variables happen to point to the same object, changing one pointer to point to another object has no effect on the other pointer.

Think of two people standing near each other. Both hold out an arm and point to the same table. Now one person turns and points to a chair. The other person isn't affected. They are still pointing to the table.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Object pointers *are* references. They just happen to be independent of each other. – Avi May 13 '16 at 07:16
0

No. Remember you are dealing with pointers. So when you do

johnsMoney = @"100";

You are setting the johnsMoney pointer to a different memory address which contains the @"100" value. marysMoney still points to the original address with the @"200" value.

drekka
  • 20,957
  • 14
  • 79
  • 135
0

In your example the local variable marysMoney maintains a strong reference to the initial johnsMoney object. When the johnsMoney property is changed, the property no longer keeps a strong reference to the original value, but that value is still kept alive by the marysMoney strong variable.

meda
  • 45,103
  • 14
  • 92
  • 122
0

@"200" is objective-c notation for an NSString object. It will have it's own memory space and johnsmoney will point to it. So, marysmoney never really points to johnsmoney.

What actually happens is this...

Johns Money 200 // pointer 1
Marys Money 200 // pointer 1
Johns Money 100 // pointer 2
Marys Money 200 // pointer 1

johnsmoney points to @"200". marysmoney also points to @"200". When johnsmoney gets assigned @"100", johnsmoney points to @"100". While marysmoney still points to @"200".

Michael Colon
  • 151
  • 1
  • 3
0

suppose the string :
@"200" pointer address : 0xeeff
@"100" pointer address : 0xeeaa

so ,your code may change like these:

 NSString *johnsMoney = @"200";
(johnsMoney = 0xeeff)
 NSString *marysMoney = johnsMoney;
(marysMoney = 0xeeff)

 johnsMoney = @"100";
(johnsMoney = 0xeeaa)
(marysMoney = 0xeeff) 

marysMoney pointer address not changed,but johnsMoney pointer address changed.

As the same:
suppose the object :
dog1 pointer address : 0xeeff

so ,your code may change like these:

Dog *dog1 = [[Dog alloc] init];
(dog1 pointer address: 0xeeff)

dog1.name = @"Dog 1";

Dog *dog2 = dog1;
(dog2 pointer address: 0xeeff)

dog1.name = @"Dog 3";
(dog1 pointer address still: 0xeeff)
(dog2 pointer address still: 0xeeff)

As they all point to the same address,they both changed。

arrfu
  • 182
  • 7
0

in simply.

NSString *johnsMoney = @"200";
//here johnsMoney is declared as NSString, so it will be string type.
//any thing given inside @"" will be considered as string.
//currently, johnsMoney contains 200.

NSString *marysMoney = johnsMoney;
//here marysMoney is declared as NSString, so it will be string type.
//johnsMoney is already a string. therefore, marysMoney automatically reads the
//string in johnsMoney. so currently, marysMoney will also be 200.

NSLog(@"Johns Money %@",johnsMoney);
NSLog(@"Marys Money %@",marysMoney);
//both will be printed as 200. right?

johnsMoney = @"100";
//now johnsMoney reassigned as 100.
//but still marysMoney is 200.
//there is nothing written for changing maryMoney from 200.

NSLog(@"Johns Money %@",johnsMoney);
NSLog(@"Marys Money %@",marysMoney);

so i think you've got it. i don't want to think it in a complicated manner by including the pointers.

Note:if any one feels that it is rubbish, kindly please avoid my answer. am sorry to post it. i just only vomited the pointer concept. i don't know how much correct is my answer.

Soorej Babu
  • 350
  • 2
  • 19
0

NSString is value type. (Immutable).

And there is a concept of sting iterning as well.

NSString *johnsMoney = @"200";
NSString *marysMoney = johnsMoney;
johnsMoney = @"100";

So when you are changing the string of johnsMoney, its now pointing to new memory address. But marysMoney still having old sting (i.e. 200) so pointing to previous address.

Go to this tutorial, you will learn really new things. https://nshipster.com/equality/

Rajneesh071
  • 30,846
  • 15
  • 61
  • 74