我有一个简单的问题。如果我像下面展示的那样声明NSString(引用类型):
答案:
NSString *johnsMoney = @"200";
NSString *marysMoney = johnsMoney;
NSLog(@"Johns Money %@",johnsMoney);
NSLog(@"Marys Money %@",marysMoney);
johnsMoney = @"100";
NSLog(@"Johns Money %@",johnsMoney);
NSLog(@"Marys Money %@",marysMoney);
生成的输出如下:
Johns Money 200
Marys Money 200
Johns Money 100
Marys Money 200
据我所知,当我将@"100"赋值给johnsMoney时,它不应该改变"marysMoney"的值为100,因为marysMoney指向johnsMoney。
更新:
我相信以下示例展示了我最初尝试做的事情:
Dog *dog1 = [[Dog alloc] init];
dog1.name = @"Dog 1";
Dog *dog2 = dog1;
NSLog(@"Dog 1 %@",dog1.name);
NSLog(@"Dog 2 %@",dog2.name);
dog1.name = @"Dog 3";
NSLog(@"Dog 1 %@",dog1.name);
NSLog(@"Dog 2 %@",dog2.name);