在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 haveproperties. 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 declareproperties
wherever methods can be declared in a class, category, or protocol declarative section. The syntax for declaring properties is:
whereattributesare one or more optional attributes (comma-separated if multiple) that affect how the compiler stores instance variables and synthesizes accessor methods. Thetypeelement specifies an object type, declared type, or scalar
type, such asid
,NSString *
,NSRange
,
orfloat
. The property must be backed by an instance variable of the same type and name.
The possibleattributes in a property declaration are listed inTable
2-1.
Table 2-1Attributes 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 ifreadonly 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 specifyretain orcopy for
properties that are objects.
|
retain
|
Specifies thatretain should
be sent to the property (which must be of an object type) upon assignment. Note thatretain is a no-op in a garbage-collected environment.
|
copy
|
Specifies thatcopy should
be sent to the property (which must be of an object type) upon assignment. The object’s class must implement theNSCopying protocol.
|
nonatomic
|
Specifies that accessor methods are synthesized as nonatomic. By default, all synthesized accessor methods areatomic: 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”inThe
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 formspropertyNamefor the getter andset
PropertyName:
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@synthesize
also 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 propertiestitle
,directReports
,
androle
, and to use thejobDescrip
instance variable to
back therole
property.
Finally, the Objective-C properties feature supports a simplified syntax foraccessing (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.
官方更详细的说明
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html%23//apple_ref/doc/uid/TP30001163-CH17
分享到:
相关推荐
iPhone开发Objective-C语法入门 iphone 入门 iphone 开发入门 iPhone开发Objective-C语法入门 iphone 入门 iphone 开发入门
Objective-C是Mac软件开发领域最主要的开发语言。假如我们对面向对象的思维已经C语言都很熟悉的话,对于我们学习Objective-C将会非常有用。
苹果iphone开发系列:Objective-C初學者指南 该种语言的最大特色是中括号【】
iOS 7 Programming Fundamentals: Objective-C, Xcode, and Cocoa Basics by Matt Neuburg (Author) Publisher: O’Reilly Media (October 2013) Language: English ISBN-10: 1491945575 ISBN-13: 978-...
只要有可能,Objective-C总是使用动态的方式来解决问题。这意味着Objective-C语言不仅需要一个编译器,同时也需要一个运行时系统来执行编译好的代码。这儿的运行时系统扮演的角色类似于Objective-C语言的操作系统,...
More iPhone Development with Objective-C is an independent companion to Beginning iPhone Development with Objective-C. That is, it is a perfect second book, but it is also a great book for those ...
Objective-C是扩展C的面向对象编程语言,也是iPhone开发用到的主要语言。本书结合理论知识与示例程序,全面而系统地讲述Objective-C编程的相关内容,包括Objective-C在C的基础上引入的特性和Cocoa工具包的功能及其中...
Title: More iPhone Development with Objective-C, 3rd Edition Author: Alex Horovitz, David Mark, Jayant Varma, Jeff LaMarche, Kevin Kim Length: 512 pages Edition: 3 Language: English Publisher: Apress ...
读者在阅读《objective-c程序设计》后,可以掌握objective-c语言的基本内容,并进行实际的iphone/ipad和mac应用开发。《objective-c程序设计》共分成11章。前6章讲述objective-c语言,包括数据类型、运算符、表达式...
在windows系统下搭建Objective-C开发环境,由于公司要做iphone开发,个人学习objc(Objective-C)语法,所以在windows7下搭建了一个objc开发环境,现把自己整理的帮助文档拿来与大家分享,希望能对大家有所帮助。...
iPhone开发资料之Objective-C语言简介,网上收集的.
Objective-C是扩展C的面向对象编程语言,也是iPhone开发用到的主要语言。 本书结合理论知识与示例程序,全面而系统地讲述Objective-C编程的相关内容,包括Objective-C在C的基础上引入的特性和Cocoa工具包的功能...
中文介绍(china-pub):Objective-C是扩展C的面向对象编程语言,也是iPhone开发用到的主要语言。. 本书结合理论知识与示例程序,全面而系统地讲述Objective-C编程的相关内容,包括Objective-C在C的基础上引入的特性和...
Objective-C Programming: The Big Nerd Ranch Guide (2nd Edition).epub (epub 格式) Want to write iOS apps or desktop Mac applications? This introduction to programming and the Objective-C language is ...
Objective-C是扩展C的面向对象编程语言,也是iPhone开发用到的主要语言。本书结合理论知识与示例程序,全面而系统地讲述Objective-C编程的相关内容,包括Objective-C在C的基础上引入的特性和Cocoa工具包的功能及其中...
《软件创富密码:iPhone应用程序开发攻略之深入浅出Objective-C2.0(适用于ios4)》系统地介绍了关于使用Objective-C2.0开发iPhone应用程序的基础知识,涵盖各种开发工具的操作技巧、框架(Framework)内部的工作原理...
elcome to Learn Objective- C on the Mac! This book is designed to teach you the basics of the Objective- C language. Objective- C is a superset of C and is the language used by many (if not most) ...
iPhone+SDK编程入门经典:使用Objective-C