OC Syntax 7--memory management @property parameters

Source: Internet
Author: User

Parameters for @property:

We already know that in order to provide convenience to the developer, OC provides the @porperty keyword, which automatically generates the property's set and get methods.

But we also know that in OC we still face the problem of object memory management, and we follow the principle of "who created, who frees" to manage memory.

So we have to rewrite the Set method: Release the original member variable (object) and then retain the new member variable.

You also have to override the ReAlloc method: Call release in ReAlloc to release the object.

Oh. The purpose of introducing @property is to let it automatically generate set and get methods, and now we have to rewrite the set method, so we have to have a new solution.

In fact, the introduction of OC @property keyword is allowed to add parameters. There are a total of 3 types of parameters. When we do not write parameters, it defaults to:

@property (atomic,readwrite,assign) book *book;

Atomic means: multi-thread security;

ReadWrite: Both the value can be read and the value can be assigned (generate set and get method);

Assign: Do not perform memory management and assign values directly to a property.

There are 3 types of parameters for @property, and the following table is all parameter explanations:

In the absence of multi-threading problems, we choose the nonatomic parameter, it runs faster.

Because we have to do memory management, we have to write the retain parameter. It will first release the original object and then retain the new object. This way, you can replace manual override of the set method of this manual labor!

@property (nonatomic,retain) book *book;

Assign,retain,copy features of three parameters:

OC Syntax 7--memory management @property parameters

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.