Objective-C NSString Reference Types

前端 未结 8 1268
情歌与酒
情歌与酒 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。

提交回复
热议问题