Setter 方法真的在 Obj-C 中做吗? [英] Setter method really do in Obj-C?

查看:58
本文介绍了Setter 方法真的在 Obj-C 中做吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一个setter方法的实现:

Here is implement of a setter method:

- (void)setCount:(NSNumber *)newCount {
    [newCount retain];
    [_count release];
    // Make the new assignment.
    _count = newCount;
}

如果_countretainCount<=0,如何释放?

If retainCount of _count is <=0, how it can release ?

推荐答案

保留计数为 0 的唯一有效对象是 nil.并且向 nil 发送任何消息只返回 0 而不做任何其他事情,所以这种情况被覆盖了.

The only valid object that can ever have a retain count of 0 is nil. And sending any message to nil just returns 0 without doing anything else, so that case is covered.

如果你的意思是这如何处理一个已释放的对象"——好吧,它不能.并且释放对象的保留计数不是真正的 0,因为该对象不再存在——它已被销毁,现在只是一块内存——所以它没有任何属性.对解除分配的对象做任何事情都是无效的,会发生什么是不确定的.

If you mean "how can this work with a deallocated object" — well, it can't. And a deallocated object's retain count isn't really 0, because the object doesn't exist anymore — it's been destroyed and is now just a chunk of memory — so it doesn't have any attributes. Doing anything with a deallocated object is invalid and what will happen is undefined.

这篇关于Setter 方法真的在 Obj-C 中做吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆