. 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