`
61party
  • 浏览: 1136032 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

iPhone开发:Objective-C属性修饰关键字使用详解

 
阅读更多

在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:

  • @property(attributes...)type propertyName

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 ifreadonlyis 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 specifyretainorcopyfor properties that are objects.

retain

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

copy

Specifies thatcopyshould be sent to the property (which must be of an object type) upon assignment. The object’s class must implement theNSCopyingprotocol.

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@synthesizefor the implementation, the compiler synthesizes getter and setter methods for the property that use simple assignment and that have the formspropertyNamefor the getter andsetPropertyName: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;

This tells the computer to synthesize accessor methods for propertiestitle,directReports, androle, and to use thejobDescripinstance variable to back theroleproperty.

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.

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


官方更详细的说明

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



分享到:
评论

相关推荐

    iPhone开发:Objective-C语法入门

    5. **Accessors(Getter & Setter)**:Objective-C提倡使用访问器方法来读写对象的属性。传统的getter和setter语法为`[photo setCaption:@"Day at the Beach"];`和`output = [photo caption];`。在Objective-C 2.0...

    iPhone开发:Objective-C语法入门.pdf

    ### iPhone开发:Objective-C语法入门 #### 一、Objective-C简介 Objective-C是一种面向对象的编程语言,它是在C语言的基础上扩展而成的。Objective-C主要应用于苹果公司的操作系统上,包括macOS、iOS、watchOS以及...

    苹果iphone开发系列:Objective-C初學者指南

    苹果iphone开发系列:Objective-C初學者指南 该种语言的最大特色是中括号【】

    iOS 7 Programming Fundamentals: Objective-C, Xcode, and Cocoa Basics

    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 基础教程(Amazon超级畅销书)英文版:Learn Objective-C on the Mac (Learn Series)

    - **定义**:Objective-C是一种面向对象的编程语言,它是对C语言的一种扩展。它主要用于Mac OS X和iOS的操作系统开发。 - **特点**:Objective-C保留了C语言的所有特性,并在此基础上增加了面向对象的特性,使得它...

    这是一个基于Objective-C语言的基础案例集 旨在用于给初学者快速了解Objective-C语言的语法 .zip

    Objective-C是一种强大的面向对象编程语言,它在Apple的Mac OS X和iOS操作系统中广泛使用,是开发iPhone、iPad应用的主要工具。本基础案例集旨在帮助初学者深入理解Objective-C的语法特性,通过实际代码示例来提升...

    iPhone开发基础教程&Objective-C.2.0程序设计

    在IT行业中,iOS应用开发是一项高需求的技能,而学习这一领域的基础往往从掌握Objective-C.2.0和iPhone开发开始。"iPhone开发基础教程&Objective-C.2.0程序设计"是为初学者量身定制的学习资源,旨在帮助他们踏入这个...

    Object-Oriented Programming with Objective-C

    - **兼容C语言**:Objective-C完全兼容C语言,可以在Objective-C程序中直接使用C语言编写的代码。 - **类与对象**:Objective-C中的类定义了对象的行为和属性,对象是类的一个实例。 ##### 3. 面向对象编程基础 - *...

    深入浅出讲objective-c

    1. **语法特性**:Objective-C是在C语言的基础上扩展的,保留了C的语法特性,同时引入了消息传递机制,使得它具有面向对象的能力。 2. **类和对象**:Objective-C中的所有数据结构都是基于类的,类是对象的模板,而...

    [Objective-C编程(第6版)]Programming in Objective-C

    - **特点**:Objective-C支持类、消息传递、继承等面向对象编程特性,并与C语言兼容,允许开发者直接在Objective-C代码中使用C代码。 ##### 2. 基础语法 - **变量和数据类型**:Objective-C支持多种内置数据类型,...

    IPhone Objective-C入门

    ### IPhone Objective-C入门知识点详解 #### 一、Objective-C简介 Objective-C是一种面向对象的编程语言,它是在C语言的基础上扩展而成的。Objective-C主要应用于苹果公司的操作系统上,包括iPhone、iPad以及Mac等...

    windows 下搭建 Objective-C 开发环境

    总结来说,在Windows系统下搭建Objective-C开发环境虽然不像在Mac OS X上那样直接便捷,但通过使用GNUstep等工具,我们依然可以实现在Windows系统下的Objective-C开发。希望这份详细的指南能够帮助那些需要或希望在...

    The Objective-C 2.0 Programming Language

    - **与C语言兼容**:Objective-C保留了C语言的所有特性,并且可以直接在Objective-C代码中使用C语言语法,这使得Objective-C易于学习和过渡。 - **强大的标准库**:Objective-C拥有丰富的标准库,包括Foundation...

    Objective-C开发语言

    1. **iOS应用开发**:Objective-C是iOS应用开发的主要语言之一,大量的iOS应用都是使用Objective-C编写的。 2. **macOS应用开发**:Objective-C同样被广泛应用于macOS的应用程序开发。 3. **游戏开发**:虽然Swift...

    Objective-C_for_iPhone_Developers.pdf

    2. **消息传递**:Objective-C 使用消息传递的方式来进行函数调用,这种方式使得代码更加清晰并且易于理解。 3. **动态性**:Objective-C 支持运行时动态绑定,这意味着可以在运行时决定方法的具体实现,这为编程...

    【Objective-C】语言简介及相关案例.pdf

    2. **iPhone应用程序开发**:Objective-C长期以来一直是开发iPhone应用程序的首选语言之一。借助于Objective-C,开发者能够在iOS平台上创建各种类型的移动应用,包括但不限于游戏、社交应用和商业应用等。 3. **...

    从C++到Objective-C

    5. **属性(Property)**:Objective-C 2.0引入了属性,这是一种声明性的方法来定义对象的实例变量和访问器方法。使用`@property`关键字可以轻松定义getter和setter方法。 #### 四、Objective-C语法概览 - **...

    From C++ to Objective-C

    - **访问控制**:Objective-C提供了`public`、`private`和`protected`关键字来控制成员的访问级别。 - **静态属性**:使用`static`关键字声明类的静态成员变量。 ##### 4.3 方法 - **原型和调用**:方法的声明和...

Global site tag (gtag.js) - Google Analytics