01 CodeReading-01-YYModel
Last updated
Was this helpful?
Last updated
Was this helpful?
感觉值对于优秀源码的学习阅读不是很够,开个坑,希望自己坚持下去 YY作者写的相关文章必看:
加上.h
只有5个文件相当轻量级的一个转模型框架,十分适合作为源码阅读计划的第一弹
YYModel.h
NSObject+YYModel.h
NSObject+YYModel.m
YYClassInfo.h
YYClassInfo.m
头文件
负责转模型的操作
对应Foundation
框架中基本类型的枚举
YYClassGetNSType(Class cls)
类型映射
返回: YYEncodingNSType
YYEncodingTypeIsCNumber(YYEncodingType type)
判断是否是C
的number
类型
返回: BOOL
NSNumber *YYNSNumberCreateFromID(__unsafe_unretained id value)
从id
类型返回NSNumber
其中对空的判断还有小数整数的处理值得一看
NSDate YYNSDateFromString(__unsafe_unretained NSString string)
从字符串获得NSDate
对象
作用域的控制值得学习 {}
Class YYNSBlockClass()
获取NSBlock
类
在判断value
类型的时候有用到
id YYValueForKeyPath(unsafe_unretained NSDictionary *dic, unsafe_unretained NSArray *keyPaths)
通过一个 keyPaths
来从字典中获取对应 value
例如 [@"id",@"ID",@"myId"]
来从dic
中获取一个ID
id YYValueForMultiKeys(unsafe_unretained NSDictionary *dic, unsafe_unretained NSArray *multiKeys)
Get the value with multi key (or key path) from dictionary
The dic should be NSDictionary
和上一个方法类似但这里的遍历时如果key
不是NSString
会调用上面的方法
NSNumber ModelCreateNumberFromProperty(unsafe_unretained id model,unsafe_unretained _YYModelPropertyMeta meta)
Get number from property.
这里位移枚举的使用值得一看
void ModelSetNumberToProperty(unsafe_unretained id model,unsafe_unretained NSNumber num,__unsafe_unretained _YYModelPropertyMeta meta)
Set number to property.
与上面的方法一对
void ModelSetValueForProperty(unsafe_unretained id model,unsafe_unretained id value,__unsafe_unretained _YYModelPropertyMeta *meta)
Set value to model with a property meta.
核心函数之一
void ModelSetWithDictionaryFunction(const void _key, const void _value, void *_context)
Apply function for dictionary, to set the key-value pair to model.
在void CFDictionaryApplyFunction(CFDictionaryRef theDict, CFDictionaryApplierFunction applier, void *context);
方法中调用
对字典中所有键值对执行applier
函数
void ModelSetWithPropertyMetaArrayFunction(const void _propertyMeta, void _context)
Apply function for model property meta, to set dictionary to model.
和上面的类似,是数组的
A property info in object model.
表示模型对象中的属性信息
对应YYClassPropertyInfo
可理解为 属性 中间件
A class info in object model.
表示模型的类信息
对应YYClassInfo
可理解为 类 中间件
其中使用了 dispatch_semaphore
信号量保证线程安全
建立了一个静态缓存,缓存了每个class
对应的_YYModelMeta
对象,提高了性能
将
Runtime
中C
的一些东西,通过面向对象的方式进行了封装,便于理解使用 转模型的基础
Type encoding's type.
列举了各类编码信息,包括值类型、方法限定类型、属性修饰类型。YYEncodingType使用掩码的方式对三类不同的枚举信息进行分类,各占据1个字节
成员变量 objc_ivar
方法 objc_method
成员属性 property_t
类 objc_class
点进去
打印
打印地址
拓展
标志
值
含义
NULL
(void *)0
C指针的字面零值
nil
(id)0
Objective-C对象的字面零值
Nil
(Class)0
Objective-C类的字面零值
NSNull
[NSNull null]
用来表示零值的单独的对象
看到这样一个宏 #define force_inline __inline__ __attribute__((always_inline))
与weak
类似但有本质不同
OC
与C
指针的转换
对内的加_
前缀
对外的不加
CoreFoundation
中也挺常见
相对于 Foundation
的方法来说,CoreFoundation
的方法有更高的性能,用 CFArrayApplyFunction()
和 CFDictionaryApplyFunction()
方法来遍历容器类能带来不少性能提升,但代码写起来会非常麻烦。
来自文章开头YY作者的文章,感觉相当重要这里Copy
来方便看
挺好的一篇参考文章: