[Objective-C] memory management of the OC midpoint syntax

Source: Internet
Author: User

Next, let's take a look at the memory management of some syntaxes.

(1) Assignment

 

  • Assign: direct value assignment, default
  • Retain: Reserved object
  • Copy: copy object

    Sample Code:

    @ Property (assign) Person * person;

    @ Synthesize person = _ person;

    When using retain:

    -(Void) setPerson :( Person *) person {

        if(_person!=person){        [_person release];        _person=[person retain];    }}

    When using copy (the object must implement the copy Protocol)

     

    -(void)setPerson:(Person *)person{    if(_person!=person){        [_person release];        _person=[person copy];    }}
    (2): read/write

     

     

     

    • Readwrite: generate the getter and setter methods. The default value is
    • Readonly: generate the getter Method

      Sample Code:

      -(Void) setPerson :( Person *) person {

      @ Synchronized (self) {_ person = person ;}// setting method

       

      (3) atomicity

       

      • Atomic: thread protection exists in a multi-threaded environment. The default value is
      • Noatomic: There is no thread protection in a multi-threaded environment.

        Sample Code:

        -(Person *) person {

            @synchronized(self){        return _person;    }    }

         

         

        Differences:

        Assign: assign values directly, just an alias.

        Retain: Keep this object. The two objects point to the same location.

        Copy: opens up a new memory space, pointing to different memory locations, the reference count is 1,

        However, it has nothing to do with the previous object (except in special cases)

Related Article

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.