Cocoa Core Competencies_accessor method

Source: Internet
Author: User
Tags instance method naming convention

Note: This series of articles translates from iOS Developer library–> Cocoa Core Competencies

The Cocoa core competencies, as the name implies, is Cocoa key concept. Just a description of each part of the concept, more detailed study, see the relevant links provided in each section.

The translator's level is limited, inevitably has the various problems, welcome to correct the exchange.

Welcome reprint, Reproduced please indicate the source: Colin

Accessor method
访问/存取 方法

An accessor method was an instance method, that gets or sets the value of a property of an object. In Cocoa's terminology, a method that retrieves the value of an object's property is referred to as a getter method, or "G etter; "A method that changes the value of an object's property was referred to as a setter method, or" setter. " These methods is often found in pairs, providing API for getting and setting the property values of a object.

存取方法是一个可以获得(get)或者设置(set)一个对象的属性值的实例方法。在Cocoa的术语中,一个检索对象属性值的方法被称为getter方法,或者"getter;",一个改变对象属性值的方法被称为setter方法,或者“setter.”这些方法通常成对建立,提供获得(get)或者设置(set)对象属性值的API。

You should use accessor methods rather than directly accessing state data because they provide an abstraction layer. Here is just the benefits that accessor methods provide:

你应该使用存取方法而不是直接存取状态数据因为他们提供了一个抽象的层。下面是存取方法所提供的两个好处:
    • You don ' t need to rewrite your code if the manner in which a property is represented or stored changes.
· 你不需要重写你的代码如果一个描述或者存取属性的方法改变。
    • Accessor methods often implement important behavior that occurs whenever a value is retrieved or set. For example, setter methods frequently implement memory management code and notify other objects when a value is changed.
· 存取方法常常实现重要行为是无论何时一个值被检索或者设置都会被重现。例如,当一个值被改变的时候, 设置方法频繁的实现内存管理代码并且通知其他对象。
Naming conventions

Because of the importance of this pattern, Cocoa defines some conventions for naming accessor methods. Given a property of type type and called name, you should typically implement accessor methods with the following form:

因为这种模式的重要性,Cocoa定义了一些命名存取方法的规则。给出一个属性的类型type和称呼name,你应该通过下面的格式,来实现存取方法:
- (type)name;- (void)setName:(type)newName;

The one exception is a property, which is a Boolean value. Here the Getter method name is Isname. For example:

有个例外是属性为Boolean值。getter方法名字应该是isName。例如:
- (BOOL)isHidden;- (void)setHidden:(BOOL)newHidden;

This naming convention was important because much other functionality in Cocoa relies upon it, in particular key-value codi Ng. Cocoa does not use getName because methods so start with ' get ' in Cocoa indicate that the method would return values by R Eference.

    这种命名惯例很重要,因为在Cocoa中的很多其他的功能都依赖它,特别是key-value coding。Cocoa不使用getName因为以get开始的方法表明方法返回引用类型的值。

Prerequisite Articles

Key-value coding

Related articles

Memory Management

Declared property

Definitive Discussion

Use Accessor Methods to Get or Set property Values in programming with OBJECTIVE-C

Cocoa Core Competencies_accessor method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.