@ property / @合成问题 [英] @property/@synthesize question

查看:119
本文介绍了@ property / @合成问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看有关内存管理的所有文档,我对某些内容感到有些困惑。

I'm going through all of my documentation regarding memory management and I'm a bit confused about something.

当你使用@property时,它会创建getters /对象的setter:

When you use @property, it creates getters/setters for the object:

.h:
@property(retain,nonatomic)NSString * myString

.h: @property (retain, nonatomic) NSString *myString

.m:
@synthesize myString

.m: @synthesize myString

我理解这一点,但我感到困惑的是使用self。我在不同的博客和书籍中看到了不同的语法。我见过:

I understand that, but where I get confused is the use of self. I see different syntax in different blogs and books. I've seen:

myString = [NSString alloc] initWithString:@"Hi there"];

self.myString = [NSString alloc] initWithString:@"Hi there"];

然后在dealloc中我看到:

Then in dealloc I see:

self.myString = nil;

[myString release];

self.myString = nil;
[myString release];

在这个网站上,有人说使用self会为保留计数增加另一个增量?是真的,我没有在任何地方见过。

On this site, someone stated that using self adds another increment to the retain count? Is that true, I haven't seen that anywhere.

提供自动释放的自动getter / setter?

Do the automatic getters/setters that are provided autorelease?

执行所有这些操作的正确方法是什么?

Which is the correct way of doing all of this?

谢谢!

推荐答案

如果你没有使用点语法你没有使用任何setter或getter。

If you are not using the dot syntax you are not using any setter or getter.

接下来是,它取决于财产的声明方式。

The next thing is, it depends on how the property has been declared.

我们假设这样的事情:

@property (nonatomic, retain) Article *article;
...
@synthesize article;

self.article = [[Article alloc] init];

将覆盖alloc / init返回的实例并导致泄漏。这是因为文章的设置者将保留它并将为您释放任何先前的实例。

will overretain the instance given back by alloc/init and cause a leak. This is because the setter of article will retain it and will release any previous instance for you.

因此您可以将其重写为:

So you could rewrite it as:

self.article = [[[Article alloc] init] autorelease];

这样做

article = [[Article alloc] init]; 

也可以,但可能涉及泄密,因为文章可能已经存在对实例的引用。因此,需要预先释放该值:

is also ok, but could involve a leak as article may hold a reference to an instance already. So freeing the value beforehand would be needed:

[article release];
article = [[Article alloc] init]; 






释放内存可以完成

[article release];

self.article = nil;

第一个直接访问该字段,不涉及setter / getters。第二个使用setter将nil设置为字段。哪个会释放当前实例,如果有一个,然后再设置为nil。

The first one does access the field directly, no setters/getters involved. The second one sets nil to the field by using a setter. Which will release the current instance, if there is one before setting it to nil.

此结构

self.myString = nil; 
[myString release];

实在是太多了,实际上发送的数据是nil,这是无害的,但也是不必要的。

is just too much, it actually sends release to nil, which is harmless but also needless.

您只需使用点语法精神映射帽子,使用访问器方法:

self.article = newArticle
// is
[self setArticle:newArticle];

myArticle = self.article;
// is
myArticle = [self article];






阅读的一些建议,所有官方Apple的文件:

Objective-C编程语言

  • Dot Syntax
  • Declared Properties

内存管理编程指南

  • Object Ownership and Disposal
  • Using Accessor Methods

这篇关于@ property / @合成问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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