objective-c 引用计数

The rules are simple:

  • Any object returned by alloccopycopyWithZone, or new has a retain count of 1.
  • retain increases the receiving object's retain count.
  • release decreases the receiving object's retain count.
  • autorelease tells the current autorelease pool to send the receiving object the release message “later”.
  • Any factory method that doesn't have “new” or “copy” in the name (e.g., stringWithString:) returns an object that it has autoreleased on your behalf.

Or, digested a bit:

  • Any method whose name contains copyallocretain, or new returns an object that you own.
  • Any method that doesn't, returns an object that you don't own.
  • To own an object, retain it.

The incorrect implementation of setName: that you show is incorrect because it stores an autoreleased object in an instance variable, when you mean to own the object. You should retain it or, in this case, copy it. One way is to simply use alloc and initWithString:, as in the correct example you show; the other way would be copy.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值