iPhone开发:Objective-C属性修饰关键字使用详解
2011-10-13 09:53
459 查看
在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:
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
or
The possible attributes in a property declaration are listed in Table
2-1.
Table 2-1 Attributes for declared properties
If you specify no attributes and specify
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
the setter.
In the
to control whether the compiler synthesizes accessor methods for particular properties. Both directives have the same general syntax:
The
loading code). The
The syntax for
example,the following statement:
This tells the computer to synthesize accessor methods for properties
and
back the
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:
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
主要关键字有如下几个:
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
id,
NSString *,
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.
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 readonlyis 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 retainor copyfor properties that are objects. |
retain | Specifies that retainshould be sent to the property (which must be of an object type) upon assignment. Note that retainis a no-op in a garbage-collected environment. |
copy | Specifies that copyshould be sent to the property (which must be of an object type) upon assignment. The object’s class must implement the NSCopyingprotocol. |
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. |
@synthesizefor
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
@implementationblocks of a class definition,you can use the
@dynamicand
@synthesizedirectives
to control whether the compiler synthesizes accessor methods for particular properties. Both directives have the same general syntax:
@dynamicpropertyName [
,propertyName2...]
;
@synthesizepropertyName [
,propertyName2...]
;
The
@dynamicdirective tells the compiler that you are implementing accessor methods for the property,either directly or dynamically (such as when dynamically
loading code). The
@synthesizedirective,on the other hand,tells the compiler to synthesize the getter and setter methods if they do not appear in the
@implementationblock.
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; |
title,
directReports,
and
role,and to use the
jobDescripinstance variable to
back the
roleproperty.
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; |
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
相关文章推荐
- Objective-C ,ios,iphone开发基础:picker控件详解与使用,(实现省市的二级联动)
- Objective-C ,ios,iphone开发基础:UIAlertView使用详解
- Objective-C ,ios,iphone开发基础:picker控件详解与使用,(实现省市的二级联动)
- Objective-C ,ios,iphone开发基础:UIAlertView使用详解
- Objective-C ,ios,iphone开发基础:picker控件详解与使用,(实现省市的二级联动)
- [置顶] Objective-C ,ios,iphone开发基础:UIAlertView使用详解
- Objective-C 的属性与合成方法使用详解
- (iPhone/iPad开发)详解UIView的center属性
- iPhone SDK开发 objective C property属性详解
- [iPhone开发之控件的使用]UIAlertView的各种属性、方法及代理的使用
- iPhone开发UIAlertView属性及使用方法
- iphone开发之表格组件UITableView的使用(五)UITableView与UItableViewCell常见属性以及单元格重用
- [iPhone开发之控件的使用]UIAlertView的各种属性、方法及代理的使用
- iPhone开发之UITextView控件使用详解
- 详解iPhone开发应用中如何使用Web视图
- Objective-C 的属性与合成方法使用详解
- [iPhone开发之控件的使用]UIActionSheet的各种属性、方法及代理的使用
- [置顶] Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作
- iPhone应用开发 SQLite数据库使用详解
- Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)