Objective-c Advanced Programming--KVC (i) __objective-c

Source: Internet
Author: User

"Basic Concepts"

1. Key-value encoding is a mechanism for indirect access to object properties, which allows access to object properties without invoking access methods and variable instances.

2. The key-value coding method is declared in the OC Informal agreement (class) Nskeyvaluecoding, and the default implementation method is provided by NSObject.

3. The key-value encoding supports properties with object values, and also supports pure numeric types and structs. Non-object parameters and return types are recognized and automatically encapsulated/marshaled.


"Key-value access"

Basic calls in key-value encodings include-valueforkey: and-setvalue:forkey: These two methods, which send message strings to objects as strings, are key to our focus on attributes. Here's a simple example of my demo:

(1) Create a new book class and add the following attributes to the header file:

@property (strong,nonatomic) NSString *name;


(2) implemented as follows in MAIN.M:

#import <Foundation/Foundation.h>
#import "Book.h"

int main (int argc, const char * argv[]) {
  @ Autoreleasepool {book

    
    *book = [[Book alloc] init];
    [Book setvalue:@ "The Old Man and the Sea" forkey:@ "name"];
    
    NSLog (@ "The name of this book is:%@", [Books valueforkey:@ "name"]);
    
    
    
  }
    return 0;
}

(3) The output results are as follows:


If there is a setter, getter method, and if not, he will find an instance variable named _key or key internally. By KVC, you can get an object value without the Getter method, without direct access through the object pointer. Here we need to note that when we pass Setvalue:forkey: Set the value of an object, or get the value of an object through Valueforkey, we need to encapsulate the data if the object's instance variable is the base data type (Char,int,float,bool).


"Path Access"

In addition to the key setting, the key-value encoding also supports the specified path, like a file system. Separated by a "dot" number. Let me show you a program:

(1) Create a new book class, in the. h header file as follows:

#import <Foundation/Foundation.h>
#import "Author.h"

@interface book:nsobject

@property (Strong, nonatomic) NSString *name;

@property (strong,nonatomic) Author *author;

@end

(2) Create a new author class, in the. h header file as follows:

#import <Foundation/Foundation.h>

@interface author:nsobject

@property (strong,nonatomic) NSString * AuthorName;

@end

(3) The following are implemented in the MAIN.M file:

#import <Foundation/Foundation.h>
#import "Book.h"
//There is no longer a need to introduce a Author.h header file, which has been introduced in Book.h.

int main (int argc, const char * argv[]) {
  @autoreleasepool {book

    
    *book = [[Book alloc] init];
    [Book setvalue:@ "The Old Man and the Sea" forkey:@ "name"];
    
    
    Author *author = [[Author alloc] init];
    [Author setvalue:@ "Hemingway" forkey:@ "AuthorName"];
    
    
    [Book Setvalue:author forkey:@ "author"];
    
    NSLog (@ "The name of this book is:%@", [Books valueforkey:@ "name"]);
    NSLog (@ "the author of this book is:%@", [Books valueforkeypath:@ "Author.authorname"]);
    
    
    [Book setvalue:@] is Ernest Hemingway? "forkeypath:@" Author.authorname "];
    NSLog (@ "the author of this book is:%@", [Books valueforkeypath:@ "Author.authorname"]);

    
  }
    return 0;
}

(4) The output results are as follows:




"Simple operation of the KVC"

In addition, you can also apply some characters to do simple operations. Sum,min,max,avg,count


"Encapsulation of basic data Types"

Setvalue:forkey: Methods and Valueforkey in KVC: methods cannot operate directly on basic data types, and need to be packaged with sample code as follows:

#import <Foundation/Foundation.h>
#import "Book.h"
//There is no longer a need to introduce a Author.h header file, which has been introduced in Book.h.

int main (int argc, const char * argv[]) {
  @autoreleasepool {book

    
    *book = [[Book alloc] init];
    [Book Setvalue:[[nsnumber alloc] initwithfloat:12.8] forkey:@ the "price"];//to be packaged here;

    NSLog (@ "books are:%@", valueforkey:@ "Price"]);
    
  }
    return 0;
}



GitHub home: https://github.com/chenyufeng1991. You are welcome to visit.












Related Article

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.