余额保留与释放? [英] Balance retain with release?

查看:92
本文介绍了余额保留与释放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是很好奇,我是否需要在其他地方添加另一个[名称发布],以与吸气剂中的保留物相匹配?

I am just curious, do I need to add a further [name release] elsewhere to match up with the retain here in the getter?

- (NSString *)name {
    return [[name retain] autorelease];
}

gary

推荐答案

我不知道您的类中的变量定义如何,但是规则是,在您的getter中,您应该为引用计数返回未更改的对象。如果呼叫方想保留引用,这就是呼叫方的责任。

I don't know how your variable definition is in your class but the rule is that in your getter you should return the object unchanged for the reference count. It's the responsability of the caller to call retain if it want to keep a reference on it.

- (NSString*) name {
  return name;
}

// caller
NSString* name = object.name;
[name retain]; // if necessary. If the string is used only in the context of a method you do not have to retain it.

如果将返回值用作另一个类中的字段,则应按以下方式定义字段:

If you are using the returned value as a field in another class you should define your field like this:

@property(retain, nonatomic) NSString* name;

使用此功能,当您分配给变量时,将调用保留。

With this a retain will be called when you assign to the variable.

这篇关于余额保留与释放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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