简体   繁体   English

iPhone开发中的Objective-C属性

[英]Objective-C Properties in iPhone Development

Whats the difference between a property and an instance variable in Objective-C. 什么是Objective-C中的属性和实例变量之间的区别。 I need to understand this in OOP terms. 我需要在OOP术语中理解这一点。 Is a property declaration just a convenience wrapper (with @synthesize in the implementation) for accessing instance variables? 属性声明只是一个方便的包装器(在实现中使用@synthesize)来访问实例变量吗?

thanks, 谢谢,

codecowboy. codecowboy。

Properties and ivars are two completely different things. 属性和ivars是两个完全不同的东西。

And instance variable is a variable stored inside the object, so each instance has its own. 而实例变量是存储在对象中的变量,因此每个实例都有自己的变量。 It is referenced by pointer addition relative to the object pointer/self (slightly indirected for the modern runtime, but functionally equivalent). 它通过相对于对象指针/ self的指针添加来引用(对于现代运行时略有间接,但在功能上等效)。 ivars are generally internal to a class, and by default can only be accessed by the class and its descendents (@protected). ivars通常是类的内部,默认情况下只能由类及其后代(@protected)访问。 Within methods they are available with no qualification, otherwise they can (but rarely are, ad usuaually should not) be accessed via indirection, eg obj->ivar. 在方法中,它们没有资格可用,否则它们可以(但很少,通常不应该)通过间接访问,例如obj-> ivar。

A property defines a getter and setter (the setter is optional) interface. 属性定义了getter和setter(setter是可选的)接口。 That's all it does. 就是这样。 It defines two public methods: 它定义了两个公共方法:

- (TYPE) propname;
- (void) setPropname: (TYPE) newPropname;

These are defined as methods exactly as if you declared them like that, no more, no less. 这些被定义为方法,就像你声明它们一样,不多也不少。 These methods are called either with the normal syntax ([obj propname] and [obj setPropname:n] or using the modern dot notation (obj.propname or obj.propname = n). These two options are syntactically different only, they behave identically, and you can use dot notation whether the methods are declared with @property or declared manually as above. 这些方法使用普通语法([obj propname]和[obj setPropname:n]或使用现代点表示法(obj.propname或obj.propname = n)调用。这两个选项在语法上不同,它们的行为相同,您可以使用点表示法,无论方法是使用@property声明还是手动声明,如上所述。

You must then implement the methods in the implementation, either by writing the methods yourself, by using @synthesize, or by handling the missing method dynamically. 然后,您必须通过自己编写方法,使用@synthesize或动态处理缺少的方法来实现实现中的方法。

Properties may be backed by an ivar (named the same or named differently (my preference to avoid confusion)), or they may not. 属性可以由ivar支持(命名相同或命名不同(我的偏好以避免混淆)),或者它们可能不支持。 They may store their value elsewhere, or they may calculate it from other data. 他们可能将其值存储在其他地方,或者他们可能从其他数据中计算出来。

For example, you might have: 例如,您可能有:

@property (nonatomic, readonly) NSString* fullname;

and then implement - (NSString*) fullname to return the concatenation of firstname and lastname. 然后实现 - (NSString *)fullname以返回firstname和lastname的串联。

I think you are pretty much there. 我觉得你差不多了。 The @property and @synthesize make the accessor declarations and implementation for the already declared ivar. @property和@synthesize为已经声明的ivar创建访问器声明和实现。 You have various attributes you can define on the @property too giving you control over how it is generated to make it appropriate for the ivar 你可以在@property上定义各种属性,让你可以控制它的生成方式,使其适合于ivar

Have a look at "Objective C 2.0 Declared Properties" 看看“Objective C 2.0声明的属性”

The difference between Property and Instance ivar is, the variable which make as Property that can be visible in another Class whereas for accessing the iVar or instance you need to create the Object of that class and then you can access. PropertyInstance ivar之间的区别在于,作为Property的变量可以在另一个Class中可见,而对于访问iVar或实例,您需要创建该类的Object然后您可以访问。 and With use of @synthesize compiler will generate the setter and getter for that property. 使用@synthesize编译器将为该属性生成setter和getter。

-(TYPE)name;-getter Method - (TYPE)name; -getter方法

-(void)setName:(TYPE)aName; - (无效)的setName:(TYPE)aName; setter Method setter方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM