object property Manipulation-ios

Source: Internet
Author: User

#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]"]);

 

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.