OC KVC总结
在iOS开发中,我们一般使用set方法或者点语法来修改对象的属性值,比如说 stu.age = 9 与 [stu setAge:9]。
KVC(key value coding)键值编码,这是一种间接修改对象属性值的方法。实现方法就是通过用字符串来描述要修改的属性。基本的操作方法有 setValue:forKey: 和 valueForKey,以字符串的形式发送对象
特别提醒,使用KVC中所有的value都必须是对象。
在此以Student类和 Book类作为例子来总结
Student类:
//.h文件
#import <Foundation/Foundation.h>
@class Book;
@interface Student : NSObject @property (nonatomic, assign) int age; @property (nonatomic, copy) NSString* name; @property (nonatomic, strong) Book* book; @property (nonatomic, strong) NSArray* books;
@end //.m文件 #import "Student.h"
#import "Book.h"
@implementation Student - (NSString *) description
{
return [NSString stringWithFormat:@"%@ %d", _name, _age];
}
@end
Book类:
//.h文件
#import <Foundation/Foundation.h> @interface Book : NSObject @property (nonatomic, assign) int price;
@end //.m文件
#import "Book.h" @implementation Book @end
主函数:
#import <Foundation/Foundation.h>
#import "Student.h"
#import "Book.h" //1.设值 取值
void fun1()
{
NSLog(@"==============fun1==============");
Student *stu = [[Student alloc] init]; [stu setValue:@"hehe" forKey:@"name"];
[stu setValue:@ forKey:@"age"]; NSLog(@"%@", stu); NSLog(@"name : %@", [stu valueForKey:@"name"]);
} //2.批量获取属性值
void fun2()
{
NSLog(@"==============fun2==============");
Student *stu = [[Student alloc] init]; [stu setValue:@"hehe" forKey:@"name"];
[stu setValue:@ forKey:@"age"]; NSArray *arr = @[@"name", @"age"]; NSDictionary *dict = [stu dictionaryWithValuesForKeys:arr]; NSLog(@"%@", dict);
} //3.批量设置属性值(通过字典批量赋值)
void fun3()
{
NSLog(@"==============fun3==============");
Student *stu = [[Student alloc] init]; NSDictionary *dict = @{@"age": @, @"name" : @"悟空"}; [stu setValuesForKeysWithDictionary:dict]; NSLog(@"%@", stu);
} //4.通过键路径(key path)访问(使用valueForKeyPath)
void fun4()
{
NSLog(@"==============fun4==============");
Student *stu = [[Student alloc] init];
Book *book = [[Book alloc] init];
stu.book = book;
//设置值
[stu setValue:@ forKeyPath:@"book.price"];
//获取值
NSNumber *price = [stu valueForKeyPath:@"book.price"]; NSLog(@"price: %@", price);
} //5.对数组进行整体操作
void fun5()
{
NSLog(@"==============fun5==============");
Student *stu = [[Student alloc] init]; Book *book = [[Book alloc] init];
book.price = ;
Book *book1 = [[Book alloc] init];
book1.price = ;
Book *book2 = [[Book alloc] init];
book2.price = ; NSArray *arr = @[book, book1, book2]; stu.books = arr;
//获取值
// NSArray *prices = [stu valueForKeyPath:@"books.price"];
NSArray *prices = [stu.books valueForKeyPath:@"price"];
NSLog(@"prices: %@", prices);
} //6.键路径的运算符(数组元素求和)
void fun6()
{
NSLog(@"==============fun6==============");
Student *stu = [[Student alloc] init]; Book *book = [[Book alloc] init];
book.price = ;
Book *book1 = [[Book alloc] init];
book1.price = ;
Book *book2 = [[Book alloc] init];
book2.price = ; NSArray *arr = @[book, book1, book2]; stu.books = arr;
//获取值
NSNumber *sum = [stu valueForKeyPath:@"books.@sum.price"];
NSLog(@"prices: %@", sum);
} //7.键路径的运算符(数组总数count)
void fun7()
{
NSLog(@"==============fun7==============");
Student *stu = [[Student alloc] init]; Book *book = [[Book alloc] init];
book.price = ;
Book *book1 = [[Book alloc] init];
book1.price = ;
Book *book2 = [[Book alloc] init];
book2.price = ; NSArray *arr = @[book, book1, book2]; stu.books = arr;
//获取值
NSNumber *sum = [stu valueForKeyPath:@"books.@count.price"];
NSLog(@"num: %@", sum);
} //8.键路径的运算符(获取数组不同元素)
void fun8()
{
NSLog(@"==============fun8==============");
Student *stu = [[Student alloc] init]; Book *book = [[Book alloc] init];
book.price = ;
Book *book1 = [[Book alloc] init];
book1.price = ;
Book *book2 = [[Book alloc] init];
book2.price = ; NSArray *arr = @[book, book1, book2]; stu.books = arr;
//获取值
NSArray *array = [stu valueForKeyPath:@"books.@distinctUnionOfObjects.price"];
NSLog(@"array: %@", array);
} //9.求数组元素中得最大值、最小值、平均值
void fun9()
{
NSLog(@"==============fun9==============");
Student *stu = [[Student alloc] init]; Book *book = [[Book alloc] init];
book.price = ;
Book *book1 = [[Book alloc] init];
book1.price = ;
Book *book2 = [[Book alloc] init];
book2.price = ; NSArray *arr = @[book, book1, book2]; stu.books = arr;
//获取值
NSNumber *max = [stu valueForKeyPath:@"books.@max.price"];
NSNumber *min = [stu valueForKeyPath:@"books.@min.price"];
NSNumber *average = [stu valueForKeyPath:@"books.@avg.price"];
NSLog(@"max: %@", max);
NSLog(@"min: %@", min);
NSLog(@"avg: %@", average);
} int main(int argc, const char * argv[])
{ @autoreleasepool {
fun1();
fun2();
fun3();
fun4();
fun5();
fun6();
fun7();
fun8();
fun9();
}
return ;
}
OC KVC总结的更多相关文章
- OC KVC
OC KVC KVC 全称 key valued coding 键值编码 在说KVC之前应该简单的了解一下反射机制 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法. 对于任意 ...
- oc kvc的模式:匹配搜索模式(模式匹配)、装包解包
按照一定规则使用匹配模式在目标空间进行搜索,然后执行相应操作: 运行时系统将kvc的运行机制解释为模式匹配,将值的兼容性问题解释为装包解包问题 一.模式匹配 The default implement ...
- iOS 头条一面 面试题
1.如何高效的切圆角? 切圆角共有以下三种方案: cornerRadius + masksToBounds:适用于单个视图或视图不在列表上且量级较小的情况,会导致离屏渲染. CAShapeLayer+ ...
- OC:属性、点语法、KVC
//属性的属性 属性定义在一个 .h文件里,在这个.h文件里可以定义实例变量(就是这个类的特征),也可以通过 @protery(属性约束关键字) 属性名字类型 属性名 来定义一些属性,在prope ...
- KVC 和 OC字典
KVC(键值编码)和OC 字典很相似,都是键值存储.但是OC 字典比较灵活,它是一种映射. [dict setObject:<#(id)#> forKey:<#(id<NSCo ...
- QF——OC中的KVC,KVO
KVC: (Key Value Coding) 键值编码 所谓KVC,其实就是不通过set和get方法访问对象属性,而是通过属性名字符串动态的去读取属性.KVC其实也是OC反射机制的一种运用. 之所以 ...
- 设计模式之观察者模式(关于OC中的KVO\KVC\NSNotification)
学习了这么久的设计模式方面的知识,最大的感触就是,设计模式不能脱离语言特性.近段时间所看的两本书籍,<大话设计模式>里面的代码是C#写的,有一些设计模式实现起来也是采用了C#的语言特性(C ...
- OC学习篇之---KVC和KVO操作
前一篇文章我们介绍了OC中最常用的文件操作:http://blog.csdn.net/jiangwei0910410003/article/details/41875015,那么今天来看一下OC中的一 ...
- [转] iOS (OC) 中 KVC 与 KVO 理解
转自: http://magicalboy.com/kvc_and_kvo/ KVC 与 KVO 是 Objective C 的关键概念,个人认为必须理解的东西,下面是实例讲解. Key-Value ...
随机推荐
- 占成本85% SSD深度选购教你如何看颗粒
颗粒是固态硬盘负责容量和传输的介质,在这一方面上与优盘产品是相同的,从外观上看,颗粒占据了整个固态硬盘内部70%左右的空间,其同样做为成本技术,根据厂商的用料不同,成为了固态硬盘内部核心材料. 颗粒的 ...
- 深入GDI图像显示
摘 要:本文首先给出了一种结合了DIB和DDB两种位图优点的图像显示方法,其次对GDI函数的高级应用,如透明位图显示.图像旋转显示.图像镜像显示进行了研究. 关键词:GDI图像显示,特殊GDI函数的 ...
- 透明窗口(窗口上面文字图片等内容不透明)的实现(使用SetLayeredWindowAttributes API函数)
透明窗口(窗口上面文字图片等内容不透明)的实现 本文讨论通过SetLayeredWindowAttributes来实现本文的目的. SetLayeredWindowAttributes的实现必须将窗口 ...
- 使WEBBROWSER 可编辑
procedure TForm1.CheckBox1Click(Sender: TObject);begin {这里是让整个页面可编辑, 也可以单独编辑某个元素} WebBrowser1.OleO ...
- 柯南君:看大数据时代下的IT架构(5)消息队列之RabbitMQ--案例(Work Queues起航)
二.Work Queues(using the Java Client) 走起 在第上一个教程中我们写程序从一个命名队列发送和接收消息.在这一次我们将创建一个工作队列,将用于分发耗时的任务在多个工 ...
- ajax与算法,sql的group处理
function correctAction(){ $semester_id = $this->_getParam("semester_id"); $day = $this- ...
- [Leetcode][Python][DP]Regular Expression Matching
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...
- linux安装php遇到的问题
第一个问题:configure: error: libjpeg.(a|so) not found 很多人都纳闷儿了,我都安装了的,为什么就找不到呢?找不到其实就应试想到它的搜索位置里面是没有的,要不然 ...
- 项目代码摘抄,dot的用法之1
function searchTags() { var list = $('#tags-list-select option:selected').val(); console.log(list); ...
- CentOS 6.2图形界面自定义开启终端快捷键
菜单: System->Preferences->Keyboard Shortcuts 在Desktop分类下找到"Run a terminal" 点击Run a te ...