属性(Property)

Properties
In many programming languages, methods that access an object’s internal state—often referred to
as getter/setter methods—must be manually coded. Objective-C provides declared properties to
both automate and simplify this task. A property differs from an instance variable in that it doesn’t
directly access an object’s internal state, but rather provides a convenient mechanism (i.e., getter/
setter methods) for accessing this data, and thus may include other logic. Objective-C declared
properties enable the compiler to generate these methods automatically according to your provided
specification. This reduces the amount of code you have to write and maintain, and increases
program consistency and reliability.


属 性
在许多编程语言中,访问对象内部的状态的方法是需要认为的编码的。Objective-c 提供了以声明属性的方式让该过程自动化并简化。
属性跟实例变量不同,它不会直接访问对象的内部状态,但提供了方便的机制(比如getter/setter)来访问这些数据。
objective-c声明属性使编译器能够自动根据你所提供描述来生成这些方法。
这样减少了大量的代码量,并且使程序更加简洁、可靠。


Property Declaration
A property is declared using the @property keyword followed by an optional set of attributes
(enclosed within parentheses), the property type, and its name.
@property (attributes) type propertyName;
For example, the Atom class you developed earlier (see Listing 2-3) declares the properties
chemicalElement, protons, neutrons, and electrons. Properties can be declared in a class interface,
a category interface, or a protocol. The declaration sets the signature for the getter/setter methods
associated with a property, but does not generate the actual method definitions.


属性声明


属性的声明方式:


@property (attributes) type propertyName;


可以看到声明属性是在@property紧跟着括号(可选项),然后属性类型,然后它的名字。


属性的声明只是对相应属性的getter/setter方法设置签名,并不会产生实际的方法定义。


例如:

Listing 2-3. Atom Interface

@interface Atom : NSObject
@property (readonly) NSUInteger protons;
@property (readonly) NSUInteger neutrons;
@property (readonly) NSUInteger electrons;
@property (readonly) NSString *chemicalElement;

- (NSUInteger) massNumber;
@end

------------------------------------------------------------------------------------------------------
Property definition


A property definition is performed in the implementation section of a class. In most cases, a property
is backed by an instance variable; hence, the property definition includes defining getter and setter
methods for the property, declaring an instance variable, and using that variable in the getter/setter
methods. Objective-C provides several methods for defining a property: explicit definition, synthesis
via keyword, and autosynthesis.


属性定义


属性是定义在一个类的实现部分。大部分情况下,一个属性支持一个实例变量。


因此,属性的定义包括属性getter/stter方法的定义,和实例变量的声明以及在getter/stter方法中使用该实例变量。


Objective-C 有多种方法定义属性:显示定义,合成器(通过关键字生成),自动合成。


-------------------------------------------------------------------------------------------------
Explicit Definition


For this method of defining a property, its methods are explicitly defined in the corresponding class
implementation. As an example, the setter method for a property named myIntProperty can be
defined as shown in Listing 2-11.


Listing 2-11. Property Setter Method Definition

-(void) setMyIntProperty:(int) value
{
	_myIntProperty = value;
}

The name of the variable corresponding to the property is prefaced with an underscore. This name
reflects the Objective-C standard naming convention for property instance variables, whereby the
name of the variable is that of the property name, prefaced with an underscore.


显示定义


这种方式定义属性是在类的实现部分进行显示定义的。
举个例子,一个叫myIntProperty的属性的setter方法可以定义成如下所示:


例2-11. 属性Setter方法的定义
-(void) setMyIntProperty:(int) value
{
	_myIntProperty = value;
}




可以看到属性相应的变量的名字是以下划线(_)作为前缀的。


变量的名字(前面带有下划线)就是属性的名字。


这是对属性实例变量的Objective-C标准的命名约定方式。


-------------------------------------------------------------------------------------------------------
Synthesis via Keyword


Through use of the @synthesize keyword, the compiler can autogenerate property definitions.
A property is synthesized in the corresponding class implementation section. The syntax for
synthesizing a property via keyword is

@synthesize propertyName [= instanceVariableName];


If the optional instanceVariableName assignment is not provided, the compiler will automatically
generate the instance variable name following the standard naming convention for property-backed
instance variables. If an instanceVariableName value is provided, the compiler will create the
instance variable with the name provided. For a property named myIntProperty, the getter and setter
methods will be autogenerated with the statement


@synthesize myIntProperty;


The compiler will create a corresponding instance variable named _myIntProperty.


通过使用关键字@synthesize, 编译器可以自动生成对属性的定义。


属性是在相应的类实现部分中合成的。合成属性的语法通过关键字:


@synthesize propertyName [= instanceVariableName];

@synthesize 属性名 [= 实例变量的名]


如果没有提供实例变量(instanceVariableName)的名字,编译器就会自动生成一个实例变量的名字,名字按照约定标准进行命名。
如果提供了实例变量的名字,编译器就会以改名字命名实例变量。


定义属性myIntProperty,getter和setter方法都会伴随下面的语句自动产生。

@synthesize myIntProperty;

同时编译器会创建一个相应的实例变量命名为_myIntProperty


----------------------------------------------------------------------------------------------------------
Autosynthesis


The recommended Apple Objective-C compiler, Clang/LLVM (versions 4.2 and above) provides
support for autosynthesis of declared properties. This means that the compiler will automatically
synthesize declared properties that are neither: 1) synthesized by keyword (e.g., using @synthesis);
2) generated dynamically (via the @dynamic property directive); nor 3) having user-provided getter
and setter methods. Thus, there is no need to include code to synthesize a declared property
when using this feature. A declared property is automatically synthesized by the compiler, along
with the corresponding instance variable. The Atom class implemented earlier (see Listing 2-3) uses
autosynthesis of its declared properties.


Listing 2-3. Atom Interface

@interface Atom : NSObject
@property (readonly) NSUInteger protons;
@property (readonly) NSUInteger neutrons;
@property (readonly) NSUInteger electrons;
@property (readonly) NSString *chemicalElement;

- (NSUInteger) massNumber;
@end



自动合成

 Objective-C 编译器建议,Clang/LLVM(版本4.2及以上)支持属性声明自动合成其定义。
 编译器自动合成属性声明(Autosynthesis)不通过以下方式:
 编译器自动合成属性声明(Autosynthesis)不通过以下方式:
1)通过关键字合成(例如使用@synthesize)
2)动态生成(通过@dynamic属性指令)
3)显示自定义getter和setter方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值