iOS Review Note 5: Set Method of memory management

Source: Internet
Author: User

1 alloc and release appear in pairs
2 Set method
2.1 Basic Type Direct Assignment
2.2 OC Object: Judge unequal, retain new, release old, and then assign value
3 Dealloc method
3.1 Be sure to call [Super Dealloc]; and put it in the last
3.2 Self-owned object to do once release


@interface Book{int _price;} -(void) Setprice: (int) price;-(int) Price; @end


@implementation book-(void) Setprice: (int) price{_price = Price;} -(int) Price{return _price;} -(void) Dealloc{[_book Release];_book = nil;[ Super Dealloc];} @end


@interface person{book* _book;} -(void) Setbook: (book*) book;-(book*) book; @end


@implementation person-(void) Setbook: (book*) book{//retain new, release old, assign new to old if (_book! = book) {[Book retain];[ _book release];_book = Book;} /* Can be abbreviated as: if (_book! = book) {[_book release];_book = [book retain];} */}-(book*) Book{return _book;} @end



#import <foundation/foundation.h>int Main () {person* person = [[Person alloc] init]; book* book = [[Book alloc] init]; [Person Setbook:book]; [Book Release];book = nil; [Person Release];return 0;}


iOS Review Note 5: Set Method of memory management

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.