常见用法:
  • 获取值

    • valueForKey: 依据属性名取值
    • valueForKeyPath: 依据路径取值(如:[person valueForKeyPath:@”car.price”])
    • valueForUndefinedKey 默认实现是抛出异常。能够重写这个函数做错误处理 ( 比較少用 )
  • 改动值
    • setValue:forKey: 依据属性设值
    • setValue:forKeyPath: 依据路径设值
    • setValue:forUndefinedKey:
    • setNilValueForKey: 当对非类对象属性设置nil时。调用。默认抛出异常
  • 字典转模型
    • setValuesForKeysWithDictionary: 字典转模型
上代码:

定义一个HSCar类

//
// 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; /** 价格 */
@property (nonatomic, assign) double price; @end

定义一个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;
/** 价格 */
@property (nonatomic, assign) double price;
@end

定义一个HSPerson类

//
// HSPerson.h
// KVC
//
// Created by hans on 15/7/13.
// Copyright © 2015年 hans. All rights reserved.
// #import <Foundation/Foundation.h>
@class HSCar; @interface HSPerson : NSObject /** 名字 */
@property (nonatomic, copy) NSString *name; /** 年龄 */
@property (nonatomic, copy) NSString *age; /** 车 */
@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

在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" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; HSPerson *person = [HSPerson new];
person.name = @"hans";
person.age = 23; person.car = [HSCar new];
person.car.name = @"兰博基尼";
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:兰博基尼 price: 10000000 /** setValue:forKey: */
[person setValue:@"hanshhh" forKey:@"name"];
[person setValue:@"100" 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:@"法拉利" 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:法拉利 price: 9999999 /** 更改私有属性 */
[person setValue:@(120) forKeyPath:@"_weight"];
[person showWeight];
// 2015-07-13 23:01:15.151 KVC[54847:892122] weight: 120.000000 /** 字典转模型 */
// 1.简单转换
NSDictionary *dict1 = @{
@"name" : @"汉斯",
@"age" : @"23",
}; [person setValuesForKeysWithDictionary:dict1];
NSLog(@"name : %@, age : %d", person.name, person.age);
// 2015-07-13 23:04:20.298 KVC[54943:895092] name : 汉斯, age : 23 // 2.复杂模型转换(注意:person.car不能转换。会变为字典对象,事实上就是简单的指针赋值而已)
NSDictionary *dict2 = @{
@"name" : @"汉斯哈哈哈",
@"age" : @"23",
@"car" : @{
@"name" : @"兰博基尼",
@"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;
// } /** 其它运算 */
HSBook *book1 = [HSBook new];
book1.name = @"结网";
book1.price = 51.0; HSBook *book2 = [HSBook new];
book2.name = @"失控";
book2.price = 40.0; HSBook *book3 = [HSBook new];
book3.name = @"异类";
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] 40
// 2015-07-13 23:20:43.435 KVC[55171:905643] 60
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

iOS KVC(Key-Value Coding)的更多相关文章

  1. xcode KVC:Key Value Coding 键值编码

    赋值 // 能修改私有成员变量 - (void)setValue:(id)value forKey:(NSString *)key; - (void)setValue:(id)value forKey ...

  2. [iOS] KVC 和 KVO

    开发iOS经常会看见KVO和KVC这两个概念,特地了解了一下. 我的新博客wossoneri.com link KVC Key Value Coding KVC是一种用间接方式访问类的属性的机制.比如 ...

  3. iOS KVC 和 KVO 区别简单总结

    KVC: key value coding,键值编码.是一种通过使用属性的名称(key)来间接访问对象属性的方法.这个方法可以不用通过 setter/getter 方法来访问对象的属性.该方法使用的实 ...

  4. iOS key value coding kvc在接收json数据与 model封装中的使用

    iOS key value coding  kvc在接收json数据与 model封装中的使用 使用 kvc 能够极大的简化代码工作,及以后的接口维护工作: 1:先创建MovieModel类.h和 . ...

  5. iOS - KVC 键值编码

    1.KVC KVC 是 Key-Value Coding 的简写,是键值编码的意思,属于 runtime 方法.Key Value Coding 是 cocoa 的一个标准组成部分,是间接给对象属性设 ...

  6. iOS kvc

    kvc在我的脑海里用来更改属性的实例变量值. 今天,他们遇到了kvc第二次去学习它,在网上看了很多博客,这似乎不符合我的口味,为了提取一些以下的.总结自己的. http://www.cnblogs.c ...

  7. iOS KVC setValuesForKeysWithDictionary的使用

    Key Value Coding Key Value Coding是cocoa的一个标准组成部分,它能让我们可以通过name(key)的方式访问property, 不必调用明确的property ac ...

  8. iOS KVC 和 KVO 的学习

    KVC  (NSKey Value Coding) :键值编码 KVO (Key Value Observing) :键值监听 前言:我曾经用过 监听 一个音频何时结束 监听视频播放 状态等 用了这种 ...

  9. iOS KVC详细讲解

    iOS KVC详细讲解 什么是KVC? KVC即NSKeyValueCoding,就是键-值编码的意思.一个非正式的 Protocol,是一种间接访问对象的属性使用字符串来标识属性,而不是通过调用存取 ...

  10. 'NSUnknownKeyException' … setValue:forUndefinedKey:]: …not key value coding compliant

    解决一个问题: 当我添加一个IBout, 报了如下错误 NSUnknownKeyException' … setValue:forUndefinedKey:]: …not key value codi ...

随机推荐

  1. plsql解决64位解决办法

    plsql解决64位解决办法 设置PLSQL Developer访问本机64位Oracle 由于在本机Windows Server 2008 R2 X64上安装了64位的Oracle 11.2.0.1 ...

  2. Class example in C/C++

    class Player {  private:   int health; //these are the attributes   int strength;   int agility;  pu ...

  3. 【1】按照Django官网,编写一个web app 创建project/配置数据库

    1. Creating a project From the command line, cd into a directory where you'd like to store your code ...

  4. 洛谷P3357 最长k可重线段集问题(费用流)

    题目描述 给定平面 x-O-yx−O−y 上 nn 个开线段组成的集合 II ,和一个正整数 kk .试设计一个算法,从开线段集合 II 中选取出开线段集合 S\subseteq IS⊆I ,使得在  ...

  5. art-template模板渲染及其过滤器

    原生语法 使用原生语法,需要导入template-native.js文件.在HTML中定义模板,注意模板的位置,不要放到被渲染区域,防止模板丢失. <script id="tpl&qu ...

  6. Excel基础视频教程在线观看

    也许你已经在Excel中完成过上百张财务报表,也许你已利用Excel函数实现过上千次的复杂运算,也许你认为Excel也不过如此,甚至了无新意.但我们平日里无数次重复的得心应手的使用方法只不过是Exce ...

  7. Django中ORM之查询表记录

    查询相关API from django.db import models # Create your models here. class Book(models.Model): title = mo ...

  8. luoguP4921 情侣?给我烧了! 组合数_容斥原理_计数问题

    Code: #include <cstdio> #include <algorithm> #include <cstring> #define setIO(s) f ...

  9. 一些BFC

    我们可能会遇到这样的一些问题,比如:子元素浮动,父元素高度塌陷:父元素跟随子元素一起移动等 这是我们可以通过触发BFC来解决这样的问题. BFC为"块级格式化上下文".它是一个独立 ...

  10. composer install或者update 出错

    composer install或者update  出错Your requirements could not be resolved to an installable set of package ...