#import <Foundation/Foundation.h> @class Author, @interface books:nsobject{ @private nsstring *color; int book_id; NSString *box; Author *author; Nsarray *relationbook; float price; @public nsstring *name; } @property (nonatomic) int size;-(void) Setbox: (NSString *) box;-(NSString *) box;-(NSString *) method; @end
#import "Books.h" @implementation books@synthesize size=_size;-(void) Setbox: (NSString *) _box{ Box=_box;} -(NSString *) box{ return box; -(NSString *) method{ return @ "This method can be called by [], or by point syntax;} @end
#import <Foundation/Foundation.h> @interface author:nsobject@property (nonatomic,copy) nsstring *name; @end
#import "Author.h" @implementation author@synthesize name=_name; @end
Books *book=[[books alloc] init]; When the property is public, it can be manipulated by pointers, KVO,KVC operation Book->[email protected] "ISO development"; NSLog (@ "%@", book->name); [Book setvalue:@ "iphone" forkeypath:@ "name"]; NSLog (@ "%@", [Book valueforkey:@ "Name"]); When the attribute is private, the Get,set method can be generated by the property and synthesize, and then the book.size=12 is manipulated by the point syntax or by invoking the automatic generation method; NSLog (@ "%d", book.size); [book setsize:14];//automatically generated set method NSLog (@ "%d", [book size]);//auto-generated Get method [book Setvalue:[nsnumber Numberwithint: 2323] forkeypath:@ "size"]; NSLog (@ "%@", [book valueforkey:@ "Size"]); When the property is private, it can be generated manually and then manipulated by point syntax or by invoking the method NSString *[email protected] "The contents of the book"; Book.box=content; NSLog (@ "%@", Book.box); [Book Setbox:content]; NSLog (@ "%@", [book Box]); [Book setvalue:@ "forkeypath:@" box "]; NSLog (@ "%@", [Book valueforkey:@ "box"]); Declares that the normal method can be called by point syntax or [] call NSLog (@ "%@", [book Method]); NSLog (@ "%@", Book.method); When the property is private, it is possible to access//-----property for normal type operation via KVO,KVC, [book setvalue:@ "Android Development" forkey:@ "name"]; [Book setvalue:@ "Whilte" forkey:@ "color"]; NSLog (@ "%@:%@", [Book valueforkey:@ "name"],[book valueforkey:@ "color"]); ----property for custom object Operation Author *author=[[author alloc] init]; [email protected] "Caicai"; [Book Setvalue:author forkey:@ "author"]; Author *author2=[book valueforkey:@ "Author"]; NSLog (@ "%@", author2.name); ----property for custom object Operation 2--Object--Path access [book setvalue:@ "Caitou" forkeypath:@ "Author.name"]; NSLog (@ "%@", [Book valueforkeypath:@ "Author.name"]); ----property for custom object operation 2--Object array--path access Nsmutablearray *relationbook=[nsmutablearray Arraywithcapacity:3]; for (int i=0; i<3; i++) {Books *book=[[books alloc] init]; NSString *name=[nsstring stringwithformat:@ "job_%d", I]; [Book Setvalue:namE forkeypath:@ "name"]; [Book setvalue:@ (12+i) forkeypath:@ "Price"]; [Relationbook Addobject:book]; } [book Setvalue:relationbook forkeypath:@ "Relationbook"]; Nsarray *names=[book valueforkeypath:@ "Relationbook.name"]; NSLog (@ "%@", names); KVC simple operation sum max avg Count NSLog (@ "%@", [Book valueforkeypath:@ "[email protected]"]); NSLog (@ "%@", [Book valueforkeypath:@ "[email protected]"]); NSLog (@ "%@", [Book valueforkeypath:@ "[email protected]"]); NSLog (@ "%@", [Book valueforkeypath:@ "[email protected]"]);