iPhone开发:ObjectiveC属性修饰关键字使用详解

在Objective-C开发中,我们几乎离不开属性,下面地文章将介绍如何正确地声明属性,并对属性的修饰关键字作详细介绍

主要关键字有如下几个:

getter=getterName

setter=setterName

nonatomic

readwrite

readonly

assign

retain

copy


@synthesize

@dynamic


下面逐一讲解

getter=getterName

指定get方法,并需要实现这个方法。必须返回与声明类型相同的变量,没有参数


setter=setterName

指定set方法,并需要实现这个方法。带一个与声明类型相同的参数,没有返回值(返回空值)

当声明为readonly的时候,不能指定set方法


nonatomic

属性默认是原子性的,非原子性主要用来解决多线程时的访问速度,提高运行效率。通常的对象类型都应该声明为非原子性的(nonatomic


readwrite

如果没有声明成readonly,那就默认是readwrite。可以用来赋值,也可以被赋值


readonly

不可以被赋值


assign

所有属性都默认assign,通常用于标量(简单变量 int, float,CGRect等)

一种典型情况是用在对对象没有所有权的时候,通常是delegate,避免造成死循环(如果用retain的话会死循环)


retain

属性必须是objc对象,拥有对象所有权,必须在dealloc中release一次。


copy

属性必须是objc对象,拥有对象所有权,必须在dealloc中release一次。且属性必须实现NSCopying协议

一般常用于NSString类型(见google objc编码指南)


@synthesize

如果不实现setter和getter方法,将按照编译器的规则自动生产setter和getter方法


@dynamic

直接或动态的执行setter和getter方法。通常自己实现setter和getter方法,基本上不会用到。


看看官方文档的介绍吧

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html%23//apple_ref/doc/uid/TP40002974-CH4-SW32


Declared Properties

In the object modeling design pattern (see “Object Modeling”) objects have properties. Properties consist of an object’s attributes, such as title and color, and an object’s relationships with other objects. In traditional Objective-C code, you define properties by declaring instance variables and, to enforce encapsulation, by implementing accessor methods to get and set the values of those variables. This is a tedious and error-prone task, especially when memory management is a concern (see “Storing and Accessing Properties”).

Objective-C 2.0, which was introduced in Mac OS X 10.5, offers a syntax for declaring properties and specifying how they are to be accessed. Declaring a property becomes a kind of shorthand for declaring a setter and getter method for the property. With properties, you no longer have to implement accessor methods. Direct access to property values is also available through a new dot-notation syntax. There are three aspects to the syntax of properties: declaration, implementation, and access.

You can declare properties wherever methods can be declared in a class, category, or protocol declarative section. The syntax for declaring properties is:

  • @property(attributes...)type propertyName

where attributes are one or more optional attributes (comma-separated if multiple) that affect how the compiler stores instance variables and synthesizes accessor methods. The typeelement specifies an object type, declared type, or scalar type, such as idNSString *NSRange, or float. The property must be backed by an instance variable of the same type and name.

The possible attributes in a property declaration are listed in Table 2-1.

Table 2-1  Attributes for declared properties

Attribute

Effect

getter=getterName

setter=setterName

Specifies the names of getter and setter accessor methods (see “Storing and Accessing Properties”). You specify these attributes when you are implementing your own accessor methods and want to control their names.

readonly

Indicates that the property can only be read from, not written to. The compiler does not synthesize a setter accessor or allow a nonsynthesized one to be called.

readwrite

Indicates that the property can be read from and written to. This is the default if readonly is not specified.

assign

Specifies that simple assignment should be used in the implementation of the setter; this is the default. If properties are declared in a non–garbage-collected program, you must specify retain or copy for properties that are objects.

retain

Specifies that retain should be sent to the property (which must be of an object type) upon assignment. Note that retain is a no-op in a garbage-collected environment.

copy

Specifies that copy should be sent to the property (which must be of an object type) upon assignment. The object’s class must implement the NSCopying protocol.

nonatomic

Specifies that accessor methods are synthesized as nonatomic. By default, all synthesized accessor methods are atomic: A getter method is guaranteed to return a valid value, even when other threads are executing simultaneously. For a discussion of atomic versus nonatomic properties, especially with regard to performance, see “Declared Properties” in The Objective-C Programming Language.

If you specify no attributes and specify @synthesize for the implementation, the compiler synthesizes getter and setter methods for the property that use simple assignment and that have the forms propertyName for the getter and setPropertyName: for the setter.

In the @implementation blocks of a class definition, you can use the @dynamic and @synthesize directives to control whether the compiler synthesizes accessor methods for particular properties. Both directives have the same general syntax:

  • @dynamic propertyName [propertyName2...];

  • @synthesize propertyName [propertyName2...];

The @dynamic directive tells the compiler that you are implementing accessor methods for the property, either directly or dynamically (such as when dynamically loading code). The@synthesize directive, on the other hand, tells the compiler to synthesize the getter and setter methods if they do not appear in the @implementation block. The syntax for @synthesizealso includes an extension that allows you to use different names for the property and its instance-variable storage. Consider, for example, the following statement:

@synthesize title, directReports, role = jobDescrip;

This tells the computer to synthesize accessor methods for properties titledirectReports, and role, and to use the jobDescrip instance variable to back the role property.

Finally, the Objective-C properties feature supports a simplified syntax for accessing (getting and setting) properties through the use of dot notation and simple assignment. The following examples show how easy it is to get the values of properties and set them using this syntax:

NSString *title = employee.title; // assigns employee title to local variable
employee.ID = "A542309"; // assigns literal string to employee ID
// gets last name of this employee's manager
NSString *lname = employee.manager.lastName;

Note that dot-notation syntax works only for attributes and simple one-to-one relationships, not for to-many relationships.

Further Reading: To learn more about declared properties, read “Declared Properties” in The Objective-C Programming Language.


官方更详细的说明

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html%23//apple_ref/doc/uid/TP30001163-CH17



原文地址:https://www.cnblogs.com/javawebsoa/p/2458456.html