Objective-C NSString Reference Types

前端 未结 8 1276
情歌与酒
情歌与酒 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: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".

提交回复
热议问题