Objective-C NSString Reference Types

前端 未结 8 1264
情歌与酒
情歌与酒 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:30

    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.

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

    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/

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