Objective-C NSString Reference Types

前端 未结 8 1366
情歌与酒
情歌与酒 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条回答
  •  萌比男神i
    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).

提交回复
热议问题