Welcome to the Csdn-markdown Editor

Source: Internet
Author: User

IOS KVC (Key-value Coding)

KVC refers to nskeyvaluecoding, an informal Protocol that provides a mechanism to indirectly access the properties of an object

Common use methods:
    • Get value

      • Valueforkey: Value based on attribute name

      • Valueforkeypath: Values based on path (e.g.[person valueforkeypath:@ "Car.price"] )

      • Valueforundefinedkey default implementation is to throw an exception, you can override this function to do error handling (less)

    • modifying values

      • Setvalue:forkey: Setting values based on attributes

      • Setvalue:forkeypath: Setting values based on path

      • Setvalue:forundefinedkey:

      • Setnilvalueforkey: Called when Nil is set on a non-class object property, throws an exception by default

    • Dictionary Turn model

      • Setvaluesforkeyswithdictionary: Dictionary Turn model
On the code:

Define a Hscar class

////  HSCar.h//  KVC////  Created by hans on 15/7/13.//  Copyright ? 2015年 hans. All rights reserved.//#import <Foundation/Foundation.h>@interface HSCar : NSObject/** 名字 */@property (nonatomic, copy) NSString *name;/** 价格 */@propertydouble price;@end

Define a Hsbook

////  HSBook.h//  KVC////  Created by hans on 15/7/13.//  Copyright ? 2015年 hans. All rights reserved.//#import <Foundation/Foundation.h>@interface HSBook : NSObject/** 书名 */@property (nonatomic, copy) NSString *name;/** 价格 */@propertydouble price;@end

Define a Hsperson class

// //HSPerson.h  //KVC  // // Created by Hans on 15/7/13.  //Copyright 2015 Hans. All rights reserved.  // #import  <foundation/foundation.h>< Span class= "hljs-annotation" > @class Hscar;  @interface  Hsperson:nsobject/** name */  @property  ( nonatomic, copy) NSString *name; /** Age */  @property  (nonatomic, copy) NSString *age; /** car */  @property  (Nonatomic, Strong) Hscar *car;  @end  
////  HSPerson.m//  KVC////  Created by hans on 15/7/13.//  Copyright ? 2015年 hans. All rights reserved.//#import "HSPerson.h"@implementation HSPerson{    // 体重    double _weight;}- (void)showWeight{    NSLog(@"weight: %f", _weight);}@end

Implemented in Viewcontroller

////VIEWCONTROLLER.M//KVC////Created by Hans on 15/7/13.//Copyright? 2015 Hans. All rights reserved.//#Import "ViewController.h"#Import "HSPerson.h"#Import "HSCar.h"#Import "HSBook.h"@interfaceViewcontroller ()@end@implementationviewcontroller-(void) Viewdidload {[SuperViewdidload]; Hsperson *person = [HspersonNew]; Person.name = @"Hans"; Person.age = at; Person.car = [HscarNew]; Person.car.name = @"Lamborghini"; Person.car.price =10000000.0;/** valueforkey: */NSLog (@"Name:%@ Age:%@", [Person valueforkey:@"Name"], [Person valueforkey:@"Age"]);//2015-07-13 22:32:02.356 kvc[54356:872650] Name:hans age:23    /** Valueforkeypath: */NSLog (@"name:%@ Price:%@", [Person valueforkeypath:@"Car.name"], [Person valueforkeypath:@"Car.price"]);//2015-07-13 22:51:27.075 kvc[54669:885579] Name: Lamborghini price:10000000    /** setvalue:forkey: */[Person setvalue:@"Hanshhh"forkey:@"Name"]; [Person setvalue:@" the"forkey:@"Age"]; NSLog (@"Name:%@ Age:%@", [Person valueforkey:@"Name"], [Person valueforkey:@"Age"]);//2015-07-13 22:53:54.876 kvc[54709:886954] name:hanshhh age:100    /** Setvalue:forkeypath: */[Person setvalue:@"Ferrari"forkeypath:@"Car.name"]; [Person setvalue:@ (9999999) forkeypath:@"Car.price"]; NSLog (@"name:%@ Price:%@", [Person valueforkeypath:@"Car.name"], [Person valueforkeypath:@"Car.price"]);//2015-07-13 22:56:36.336 kvc[54767:888926] Name: Ferrari price:9999999    /** Changing private properties * /[Person setvalue:@ ( -) forkeypath:@"_weight"]; [Person Showweight];//2015-07-13 23:01:15.151 kvc[54847:892122] weight:120.000000    /** Dictionary Turn model * /    //1. Simple ConversionNsdictionary *dict1 = @{@"Name": @"Hans",                           @"Age": @"All",                           };    [Person Setvaluesforkeyswithdictionary:dict1]; NSLog (@"Name:%@, Age:%d", Person.name, Person.age);//2015-07-13 23:04:20.298 kvc[54943:895092] Name: Hans, Age:23    //2. Complex model conversion (note: Person.car cannot be converted, it becomes a Dictionary object, it is simply a pointer assignment)Nsdictionary *dict2 = @{@"Name": @"Hansha haha",                           @"Age": @"All",                           @"Car": @{                                   @"Name": @"Lamborghini",                                   @"Price": @"10000000"},                            };    [Person Setvaluesforkeyswithdictionary:dict2]; NSLog (@"%@", Person.car);//2015-07-13 23:10:32.310 kvc[55019:899474] {//name = "\U5170\U535A\U57FA\U5C3C";//Price = 10000000;//    }    /** Other Operations * /Hsbook *book1 = [HsbookNew]; Book1.name = @"Netting"; Book1.price =51.0; Hsbook *BOOK2 = [HsbookNew]; Book2.name = @"Out of Control"; Book2.price =40.0; Hsbook *BOOK3 = [HsbookNew]; Book3.name = @"Heterogeneous"; Book3.price =60.0;    Person.books = @[book1, Book2, BOOK3]; NSLog (@"%@", [Person.books valueforkeypath:@"@count"]); NSLog (@"%@", [Person.books valueforkeypath:@"@avg. Price"]); NSLog (@"%@", [Person.books valueforkeypath:@"@min. Price"]); NSLog (@"%@", [Person.books valueforkeypath:@"@max. Price"]);//2015-07-13 23:20:43.434 kvc[55171:905643] 3//2015-07-13 23:20:43.434 kvc[55171:905643] 50.333333333333333333333333333333333333//2015-07-13 23:20:43.435 kvc[55171:905643]//2015-07-13 23:20:43.435 kvc[55171:905643]}- (void) Didreceivememorywarning {[SuperDidreceivememorywarning];//Dispose of any resources, can be recreated.}@end

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Welcome to the Csdn-markdown Editor

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.