Custom class property settings and internal implementations of setter and Getter methods

Source: Internet
Author: User

A property is one embodiment of encapsulation in an object-oriented language, and setting a property in a custom class is equivalent to defining a private variable, a setter method, and an accessor (getter method), where the definition of the variable is The Declaration and implementation of the method is automatically generated by the system and is managed and optimized accordingly, without the need for developers to worry about it.

Declaration of the property:

Attributes are generally defined in the interface of a class, or within the extension of a class, and before the declaration of a method, before the declaration of a member variable. The attribute keyword is @property, the attribute in parentheses is the property, then the type and name of the variable, the definition of the property is generally divided into two cases, a non-object type of variable, such as int integer, using assign, the second is the object type of the variable, using retain or copy, If the variable of the object type is a proxy, use assign.

Properties of the property include:

1. Atomicity Properties:

Nonatomic: Non-atomic, read and write faster but not secure in multithreading.

Atomic: Atomicity, in multi-threaded at the same time only allow one thread to access, to ensure data security. Default.

2. Semantic attributes:

Assign: Direct assignment for basic data types and proxies.

Retain: Assignment is to make the reference count plus 1 for all object types except the agent.

Copy: Creates a copy when assigned, applies to all object types except the agent, but consumes more memory and is used with caution.

3. Read and Write properties:

ReadWrite: Read-write, automatic generation of the setup and accessors. Default

ReadOnly: Read only, generate accessors only.

Getter= Method Name: Specifies the accessor's method name.

Setter= Method Name: Specifies the method name of the setting.

Point syntax:

Per.name=? Equivalent to invoking the property name setter method,? =per.name or Per.name is equivalent to the Getter method that invokes the Name property.

Multi-Focus Syntax: First the Per object has a Student* object with the Name property of the nsstring* type

In this case, the getter method of the Per property Stu is called First, and then the setter method of the Stu property name is called.

The internal implementation and rewriting of the property setter method and getter method:

Take @property (assign,nonatomic) int age; For example

Setter method:-(void) Setage: (int) age;

Getter Method:-(int) age;

Internal implementation: The property is declared by default as a private variable named property name preceded by an underscore such as _age, while by default only one of the setter and getter can be overridden, to override both to implement the file plus

@synthesize keywords, such as @synthesize age = _age;

@property (assign,nonatomic) int age;

@property (retain,nonatomic) NSString *name;

@property (copy,nonatomic) NSString *sex;

A property set to assign is directly assigned, not necessarily in detail, and properties set to retain or copy are memory-managed and optimized, used generally for object types, and proxies use assign because the agent itself does not own the ownership of the agent, so it does not need to operate on the agent's memory , you can assign a value directly, but instead of the proxy, because the object holds ownership of its own private variable, memory management of the private variable is required. In the setter method, if the memory consumed by the old value is not freed, it will cause a memory leak, and if the new value is not retain or copied, the property will become a wild pointer when it is released outside the method used in the argument. In the Getter method, if you do not retain or copy the return, when the object is released there will be a wild pointer, if not autorelease will cause a memory leak.

Custom class property settings and internal implementations of setter and Getter methods

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.