一只笨羊 2014-05-05 01:14 采纳率: 0%
浏览 2129

objective-c关于引用计数问题

#import

@interface ViewController : UIViewController

@property(nonatomic,retain)NSString *string1;

@property(nonatomic,retain)NSString *string2;

@end

@implementation ViewController

  • (void)viewDidLoad

{

[super viewDidLoad];

NSLog(@"%d",[self.string1 retainCount]);

NSLog(@"%d",[self.string2 retainCount]);

self.string1=[NSString stringWithFormat:@"test"];

NSString *s2=[NSString stringWithFormat:@"test"];

NSLog(@"%d",[s2 retainCount]);

NSLog(@"%d",[self.string1 retainCount]);

NSLog(@"%d",[self.string2 retainCount]);

self.string2=self.string1;

NSLog(@"%d",[self.string1 retainCount]);

NSLog(@"%d",[self.string2 retainCount]);

}
小弟一直对引用计数比较晕 打印的结果分别是 0 0 1 2 0 3 3
我很纳闷为什么
NSLog(@"%d",[s2 retainCount]);
self.string1=[NSString stringWithFormat:@"test"]; 这俩个引用计数为什么是 1 2 为什么不都是1
还有最后的俩句
NSLog(@"%d",[self.string1 retainCount]);
NSLog(@"%d",[self.string2 retainCount]); 我觉得 string2引用string1 那么string1的引用计数加1 string2的引用计数为什么是3了 求大神解答 太晕了

  • 写回答

1条回答 默认 最新

  • allinwind3344 2016-05-12 02:44
    关注

    self.string1=[NSString stringWithFormat:@"test"],[NSString stringWithFormat:@"test"]的retain count 是1,然后赋值self.string1,此时调用了 属性string1的set 方法,而string1定义成retain, 所以retain count会加1,变成2

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题