什么时候必须在iPhone SDK中使用@property和@synthesize? [英] When do you have to use @property and @synthesize in the iPhone SDK?

查看:103
本文介绍了什么时候必须在iPhone SDK中使用@property和@synthesize?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您何时必须在iPhone SDK中使用 @property @synthesize ?为什么要使用 @property @synthesize
我正在学习房产,但我无法得到正确的想法。用于说明这一点的一些例子是什么?

When do you have to use @property and @synthesize in the iPhone SDK? And why use @property and @synthesize? I was studying property, but I can't get a correct idea. What would some examples for illustrating this be?

推荐答案

@property:您在以下时间使用它:

@property : you used it when you want to:

您可以使用一些非常有用的生成代码,如非原子,atmoic,retain,而无需编写任何代码行。你也有getter和setter方法。要使用它,还有另外两种方法:@synthesize或@dynamic:@synthesize,编译器会自动为你生成getter和setter,@动态:你必须自己编写它们。

You can use some of the really useful generated code like nonatomic, atmoic, retain without writing any lines of code. You also have getter and setter methods. To use this, you have 2 other ways: @synthesize or @dynamic: @synthesize, compiler will generate the getter and setter automatically for you, @dynamic: you have to write them yourself.

@property非常适合内存管理,例如:retain。

@property is really good for memory management, for example: retain.

如果没有@property,你怎么能保留?

How can you do retain without @property?

  if (_variable != object) {
    [_variable release];
    _variable = nil;
    _variable = [object retain];
  }

如何在@property中使用它?

How can you use it with @property?

self.variable = object;

当你调用上面一行时,你实际上像[self setVariable:object]那样调用setter然后生成的setter将完成它的工作

When you are calling the above line, you actually call the setter like [self setVariable:object] and then the generated setter will do its job

这篇关于什么时候必须在iPhone SDK中使用@property和@synthesize?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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