Objective-C NSString Reference Types

前端 未结 8 1256
情歌与酒
情歌与酒 2021-01-14 17:35

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

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

           


        
相关标签:
8条回答
  • 2021-01-14 18:16

    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。

    0 讨论(0)
  • 2021-01-14 18:16

    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.

    0 讨论(0)
  • 2021-01-14 18:18

    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).

    0 讨论(0)
  • 2021-01-14 18:21

    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.

    0 讨论(0)
  • 2021-01-14 18:24

    @"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".

    0 讨论(0)
  • 2021-01-14 18:27

    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.

    0 讨论(0)
提交回复
热议问题