iOS底层学习-KVC使用实践以及实现原理
简介
KVC(Key-value coding)键值编码,顾名思义。额,简单来说,是可以通过对象属性名称(Key)直接给属性值(value)编码(coding)“编码”可以理解为“赋值”
。这样可以免去我们调用getter和setter方法,从而简化我们的代码,也可以用来修改系统控件内部属性(这个黑魔法且用且珍惜
)。
1. 最简单的使用例子
- 假设有
CYXModel
类与CYXShopModel
类,CYXModel
里面有name
、product
属性,CYXShopModel
里面有productName
属性。
@interface CYXModel: NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) CYXShopModel *product;
@end
@interface CYXShopModel: NSObject
@property (nonatomic, strong) NSString * productName;
@end
- 不使用KVC,我们这样访问
CYXModel
的属性- 取值:
CYXModel *model = [[CYXModel alloc]init];
NSString *name = model. name;
CYXShopModel *shop = model. product;
NSString *productName = shop. productName;
- 设值:
CYXModel *model = [[CYXModel alloc]init];
model. name = @"CYX";
CYXShopModel *shopModel = [[CYXShopModel alloc]init];
shopModel. productName = @"NIKE";
model. product = shopModel;
- 使用KVC,我们可以这样访问
CYXModel
的属性 - 取值:
CYXModel *model = [[CYXModel alloc]init];
NSString *name = [model valueForKey: @"name" ];
NSString *productName = [model valueForKeyPath: @"product.productName" ];
- 设值:
CYXModel *model = [[CYXModel alloc]init];
[model setValue:@"CYX" forKey:@"name"];
[model setValue:@"NIKE" forKeyPath:@"product.productName"];
注: 这个简单的例子,可能你看了觉得这并没什么卵用,下面我们来分析一下稍微有点卵用的例子吧。
2. KVC字典转模型的实现原理
假设dict字典中有name,icon的Key,CYXModel模型类中必须要有同名的name,icon属性与之相对应。
我们使用
[CYXModel setValuesForKeysWithDictionary:dict];
进行字典转模型。setValuesForKeysWithDictionary:
方法内部实现原理如下:- (1) 遍历字典里面所有的key和值,name,icon。
// enumerateKeysAndObjectsUsingBlock:遍历字典中的所有keys和valus
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
// 利用KVC给模型中属性赋值,,
// key:用来给哪个属性
// Value:给模型的值
[CYXModel setValue:obj forKey:key];
}];
- (2) 分别给属性赋值
[CYXModel setValue:dict[@"name"] forKey:@"name"];
[CYXModel setValue:dict[@"icon"] forKey:@"icon"];
setValue:forKey:
方法:给模型的属性赋值- 赋值原理:
- (1)去模型中查找有没有
setIcon
方法,就直接调用这个set方法,给模型这个属性赋值[self setIcon:dict[@"icon"]];
- (2)如果找不到set方法,接着就会去寻找有没有
icon
属性,如果有,就直接访问模型中icon = dict[@"icon"];
- (3)如果找不到icon属性,接着又会去寻找
_icon
属性,如果有,直接_icon = dict[@"icon"];
- (4)如果都找不到就会报错
[<Flag 0x7fb74bc7a2c0> setValue:forUndefinedKey:]
- (1)去模型中查找有没有
- 赋值原理:
扩展:读者可以去查查KVV(键值验证),进一步理解报错原因与容错方法。
注: 稍微有点卵用的看完,接下来说一个比较有卵用的用法,这个例子需要配合runtime来实现,有兴趣可以看看,runtime内容不少,这里就暂不介绍了,欢迎 关注,在下篇文字小结一下runtime。
3. 修改系统控件内部属性(runtime + KVC)
有时候,UI会闲着没事,会给你找点事情,例如,界面设计图是这样的:
这。。怎么感觉有点不同,这
UIPageControl
怎么跟我平常用的不一样?平常不都是这样的??如下图
首先想到的肯定是,查看
UIPageControl
的头文件,如下:
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIPageControl : UIControl
@property(nonatomic) NSInteger numberOfPages; // default is 0
@property(nonatomic) NSInteger currentPage; // default is 0. value pinned to 0..numberOfPages-1
@property(nonatomic) BOOL hidesForSinglePage; // hide the the indicator if there is only one page. default is NO
@property(nonatomic) BOOL defersCurrentPageDisplay; // if set, clicking to a new page won't update the currently displayed page until -updateCurrentPageDisplay is called. default is NO
- (void)updateCurrentPageDisplay; // update page display to match the currentPage. ignored if defersCurrentPageDisplay is NO. setting the page value directly will update immediately
- (CGSize)sizeForNumberOfPages:(NSInteger)pageCount; // returns minimum size required to display dots for given page count. can be used to size control if page count could change
@property(nullable, nonatomic,strong) UIColor *pageIndicatorTintColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
@property(nullable, nonatomic,strong) UIColor *currentPageIndicatorTintColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
@end
- 卧槽,就这么几个属性可以给我设的,不够用啊兄弟。能不能给我个可以赋值
UIImage
对象的属性?看来正常途径使用系统的控件是设不了了,剩下的我感觉只有两种方法(如有其它,欢迎指出),一种是自定义PageControl
,这种方式看起来不简单,各位有兴趣可以去试试。另一种方式就是,通过runtime遍历出UIPageControl
所有属性(包括私有成员属性,runtime确实很强大)。 - 使用runtime遍历
UIPageControl
结果(下篇文字再谈谈runtime,这里暂不解释)如下打印:
2016-03-23 01:09:26.161 TenMinDemo[6224:507269] UIPageControl -> _lastUserInterfaceIdiom = q
2016-03-23 01:09:26.161 TenMinDemo[6224:507269] UIPageControl -> _indicators = @"NSMutableArray"
2016-03-23 01:09:26.161 TenMinDemo[6224:507269] UIPageControl -> _currentPage = q
2016-03-23 01:09:26.161 TenMinDemo[6224:507269] UIPageControl -> _displayedPage = q
2016-03-23 01:09:26.162 TenMinDemo[6224:507269] UIPageControl -> _pageControlFlags = {?="hideForSinglePage"b1"defersCurrentPageDisplay"b1}
2016-03-23 01:09:26.162 TenMinDemo[6224:507269] UIPageControl -> _currentPageImage = @"UIImage" // 当前选中图片
2016-03-23 01:09:26.162 TenMinDemo[6224:507269] UIPageControl -> _pageImage = @"UIImage" // 默认图片
2016-03-23 01:09:26.162 TenMinDemo[6224:507269] UIPageControl -> _currentPageImages = @"NSMutableArray"
2016-03-23 01:09:26.162 TenMinDemo[6224:507269] UIPageControl -> _pageImages = @"NSMutableArray"
2016-03-23 01:09:26.162 TenMinDemo[6224:507269] UIPageControl -> _backgroundVisualEffectView = @"UIVisualEffectView"
2016-03-23 01:09:26.162 TenMinDemo[6224:507269] UIPageControl -> _currentPageIndicatorTintColor = @"UIColor"
2016-03-23 01:09:26.163 TenMinDemo[6224:507269] UIPageControl -> _pageIndicatorTintColor = @"UIColor"
2016-03-23 01:09:26.163 TenMinDemo[6224:507269] UIPageControl -> _legibilitySettings = @"_UILegibilitySettings"
2016-03-23 01:09:26.163 TenMinDemo[6224:507269] UIPageControl -> _numberOfPages = q
- 结果非常满意,果然找到我想要的图片设置属性。
- 然后通过KVC设置自定义图片,实现了效果,代码如下:
UIPageControl *pageControl = [[UIPageControl alloc] init];
[pageControl setValue:[UIImage imageNamed:@"home_slipt_nor"] forKeyPath:@"_pageImage"];
[pageControl setValue:[UIImage imageNamed:@"home_slipt_pre"] forKeyPath:@"_currentPageImage"];
- 注:
这里只是抛砖引玉的讲了个小例子,其他的神奇功能等待读者去发现啦。
提示: 在xib/Storyboard中,也可以使用KVC,下面是在xib中使用KVC把图片边框设置成圆角
iOS底层学习-KVC使用实践以及实现原理的更多相关文章
- iOS 反射 学习 和 运用
iOS 反射 学习 和 运用 反射: 通过 类名来获得生成的相应的类的实例 的这种机制 叫 反射 常用的反射方式 把 NSDictionary 转成 自定义 model 自定义 model 转 ...
- iOS底层音频处理技术(带源代码)
本文由论坛会员artgolff分享 前几天搜索资料时发现一个网站: iPhone Core Audio Development ,里面有iOS底层 音频 技术的几个源 代码 ,如果你要实现VoIP电话 ...
- IOS基础学习-2: UIButton
IOS基础学习-2: UIButton UIButton是一个标准的UIControl控件,UIKit提供了一组控件:UISwitch开关.UIButton按钮.UISegmentedContro ...
- iOS阶段学习第一天笔记(Mac终端的操作)
前言部分 原本从事的是.NET开发,一直在要不要转iOS 中犹豫徘徊,经过复杂的内心挣扎终于鼓起勇气辞职脱产学习iOS;希望通过四个月的 学习后能够拿到理想的薪资.以下是学习过程中的学习笔记,为了方便 ...
- ios网络学习------4 UIWebView的加载本地数据的三种方式
ios网络学习------4 UIWebView的加载本地数据的三种方式 分类: IOS2014-06-27 12:56 959人阅读 评论(0) 收藏 举报 UIWebView是IOS内置的浏览器, ...
- ios网络学习------6 json格式数据的请求处理
ios网络学习------6 json格式数据的请求处理 分类: IOS2014-06-30 20:33 471人阅读 评论(3) 收藏 举报 #import "MainViewContro ...
- iOS之学习资源收集--很好的IOS技术学习网站
点击图片也能打开相关的网站: https://boxueio.com/skill/swift http://ios.b2mp.cn/ http://gold.xitu.io/welcome/?utm_ ...
- ios开发之OC基础-ios开发学习路线图
本系列的文章主要来自于个人在学习前锋教育-欧阳坚老师的iOS开发教程之OC语言教学视频所做的笔记,边看视频,边记录课程知识点.建议大家先过一遍视频,在看视频的过程中记录知识点关键字,把把握重点,然后再 ...
- iOS手势学习UIGestureRecognizer & cocos2d 手势推荐
iOS手势学习UIGestureRecognizer & cocos2d 手势推荐 手势识别类型: UILongPressGestureRecognizer // 长按UIPanGestur ...
随机推荐
- 编写高质量代码:改善Java程序的151个建议(第二章:基本类型)
编写高质量代码:改善Java程序的151个建议(第二章:基本类型) 目录 建议21:用偶判断,不用奇判断 建议22:用整数类型处理货币 建议23:不要让类型默默转换 建议24:边界还是边界 建议25: ...
- [leetcode-532-K-diff Pairs in an Array]
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...
- 解决发http get请求的时候不成功,出现android.os.NetworkOnMainThreadException的异常
问题描述:在接游戏sdk的时候,由于游戏要求购买的时候是在主线程里面进行的,但是发http请求是不能在主线程里面发,否则就会出现android.os.NetworkOnMainThreadExcept ...
- PILLOW图片中加入中文 曲线救国Opencv
索引 简述 准备 示例 效果图 结语 简述 我在使用opencv2或3的时候想要在图片上添加中文文字,需要去下载Freetype库,编译好链接到opencv库中才能中文的输出.网上大部分在图片中插入中 ...
- if和for的应用
语句 顺序 结束加分号 分支 让程序根据条件不同执行不同的代码 if语句 if(条件){代码} if(条件){代码}else{代码} else if(条件){代码} if嵌套 switch...cas ...
- Object-C知识点 (三) 单例 蒙版 刷新 KVO底层
#pragma mark - 单例方法(完整的方法) 系统的单例方法名称 sharedApplication defaultManager standardUserDefaults currentDe ...
- 手机端 图片的移动缩放旋转兼容touch
//缩放var initialScale = 1;var currentScale = 1;touch.on('#target', 'pinch', function (ev) { currentSc ...
- Android各种Manager
一.PowerManager 主要是用来控制电源状态,设置屏幕状态,和电池待机状态 PowerManager pm = ((PowerManager)getSystemService(POWER_S ...
- 二、Solr单机版的搭建
1.1. 运行环境 solr 需要运行在一个Servlet容器中,Solr4.10.3要求jdk使用1.7以上,Solr默认提供Jetty(java写的Servlet容器),本次使用Tocmat作为S ...
- (转载)JProfiler试用手记
JProfiler是一款Java的性能监控工具.可以查看当前应用的对象.对象引用.内存.CPU使用情况.线程.线程运行情况(阻塞.等待等),同时可以查找应用内存使用得热点,这里提供有几篇文章供参考:获 ...