Internal implementation methods for different types of assgin, retain, and copy

Source: Internet
Author: User

. H file .......

@ Interface book: nsobject/* {nsstring * _ bookname; // name of the book cgfloat _ bookthickness; // thickness nsstring * _ booktype; // book type nsinteger _ bookprice; // book price nsstring * _ publishinghouse; // press nsstring * _ publishinttime; // publication time} * // In @ property, the instance variables are defined, setter method and getter method. You do not need to define the instance variable @ property (nonatomic, copy) nsstring * bookname; // use copy to write the complete attribute @ property (nonatomic, assign) cgfloat bookthickness; @ property (nonatomic, retain) nsstring * booktype; // retain is not recommended. Most use copy @ property (nonatomic, assign) nsinteger bookprice; @ property (nonatomic, copy) nsstring * publishinghouse; @ property (nonatomic, copy) nsstring * publishinttime;-(void) read;-(void) write; @ end


. M file

@ Implementation book @ synthesize bookname = _ bookname; @ synthesize bookthickness = _ bookthickness; @ synthesize booktype = _ booktype; @ synthesize bookprice = _ bookprice; @ synthesize publishinghouse = _ publishinghouse; @ synthesize publishinttime = _ publishinttime;-(void) setbookname :( nsstring *) bookname {If (_ bookname! = Bookname) {[_ bookname release]; // auto --- all --- combined --- language -- Objective C ---> NO here is a set of memory, _ bookname = [bookname copy]; }}- (nsstring *) bookname {return [[_ bookname retain] autorelease];}-(void) setbookthickness :( cgfloat) bookthickness {_ bookthickness = bookthickness;}-(cgfloat) bookthickness {return _ bookthickness;}-(void) setbooktype :( nsstring *) booktype {If (_ booktype! = Booktype) {[_ booktype release]; _ booktype = [booktype retain]; // the unique difference between retain and copy is found here. }-(Nsstring *) booktype {return [[_ booktype retain] autorelease];}-(void) setbookprice :( nsinteger) bookprice {_ bookprice = bookprice;}-(nsinteger) bookprice {return _ bookprice;}-(void) setpublishinghouse :( nsstring *) publishinghouse {If (_ publishinghouse! = Publishinghouse) {[_ publishinghouse release]; _ publishinghouse = [publishinghouse copy] ;}- (nsstring *) publishinghouse {return [[_ publishinghouse retain] autorelease];} -(void) setpublishinttime :( nsstring *) publishinttime {If (_ publishinttime! = Publishinttime) {[_ publishinttime release]; _ publishinttime = [publishinttime copy] ;}- (nsstring *) publishinttime {return [[_ publishinttime retain] autorelease];} -(void) read; {nslog (@ "this is a book");}-(void) Write {nslog (@ "");} @ end



This article from the "Liu _ blog" blog, please be sure to keep this source http://liuyafang.blog.51cto.com/8837978/1543816

Internal implementation methods for different types of assgin, retain, and copy

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.