JFModel

真的好久好久没写点东西了,先对这段时间的总结吧。

首先对技术的热忱从未消减,但是总是什么都想学,什么都是半途而废,比如python,H5都是自学一段时间后 就没有继续下去,这是病得治。
再说说锻炼吧,最近有一个很有成就感的就是 学会的蛙泳,从刚开始的各种喝水到现在游轻松游200米不是问题,最近又开始解锁自由泳。。。
发现一个很有有趣的东西,学游泳和学技术有很多相似的地方,比如说:刚开始的时候,觉得很兴奋,啥都要都要尝试下,然后就不在意 腿部动作,不在意腿部动作,不在意手的动作,不在意呼吸节奏,然后就各种喝水呛水,等自己会游一点了就很有成就感,就想游的标准,游的更远。学技术,学新的语言也是一样的。刚开始很兴奋,然后就浑沦吞枣,结果处处碰壁,到沮丧,到学到一点东西就有点成就感的过程真的和游泳很像。

好了回归正题。
看到这个标题差不多就有点感觉 这次和大家分享的东西。没错就是一个常用并且现在网上很多类似的框架,如:MJExtension , YYModel,Mantle等等 字典转模型框架。
接下来,我会说下我的JFModel的实现过程,和用法。

实现过程:

首先我们看下数据结构 User.h 这是一个

用户模型

typedef enum {
    SexMale,
    SexFemale
} Sex;

@interface User : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *icon;
@property(nonatomic,copy)NSString *age;
@property(nonatomic,copy)NSString *height;
@property(nonatomic,copy)NSString *money;
@property(nonatomic,copy)NSString *sex;
@property(nonatomic,copy)NSString *gay;

@end

字典

NSDictionary *dict = @{
                           @"name" : @"linjianfang",
                           @"icon" : @"jf.png",
                           @"age" : @"20",
                           @"height" : @1.75,
                           @"money" : @"1000009.9999",
                           @"sex" : @(SexFemale),
                           @"gay" : @"true",
                           };

我们想要的结果如下:

    NSLog(@"name=%@, icon=%@, age=%zd, height=%@, money=%@, sex=%d, gay=%d", user.name, user.icon, user.age, user.height, user.money, user.sex, user.gay);

好,我们先明确是目的,用模型里面的属性名做key去字典里面找出对应的value,用模型的属性的类型将值转为正确的类型。
因为模型大多情况下都是纯净的都是继承NSobject的 所以我们就建一个NSobject的分类

/** 
 * Describes the properties declared by a class.
 * 
 * @param cls The class you want to inspect.
 * @param outCount On return, contains the length of the returned array. 
 *  If \e outCount is \c NULL, the length is not returned.        
 * 
 * @return An array of pointers of type \c objc_property_t describing the properties 
 *  declared by the class. Any properties declared by superclasses are not included. 
 *  The array contains \c *outCount pointers followed by a \c NULL terminator. You must free the array with \c free().
 * 
 *  If \e cls declares no properties, or \e cls is \c Nil, returns \c NULL and \c *outCount is \c 0.
 */
OBJC_EXPORT objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);

Putting these together, you can print a list of all the properties associated with a class using the following code:

image.png

我们打印出来这个user属性的名字就是我们定义的属性的名字,属性的类型是T开始和后面的一些东西,等会下面会解释这个个什么东东

name:name---attributes:T@"NSString",C,N,V_name
name:icon---attributes:T@"NSString",C,N,V_icon
name:age---attributes:TI,N,V_age
name:height---attributes:T@"NSString",C,N,V_height
name:money---attributes:T@"NSNumber",&,N,V_money
name:sex---attributes:Ti,N,V_sex
name:gay---attributes:Tc,N,GisGay,V_gay

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html

Property Type String

You can use the property_getAttributes function to discover the name, the @encode type string of a property, and other attributes of the property.

The string starts with a T followed by the @encode type and a comma, and finishes with a V followed by the name of the backing instance variable. Between these, the attributes are specified by the following descriptors, separated by commas:

image.png

我们取出每个type 所对应的 code 拿到这些code我们就可以知道这是什么类型

- (void)getTypeCode:(NSString *)code
{
    if ([code isEqualToString:JFPropertyTypeId]) {
        _idType = YES;
    } else if (code.length > 3 && [code hasPrefix:@"@\""]) {
        // 去掉@"和",截取中间的类型名称
        _code = [code substringWithRange:NSMakeRange(2, code.length - 3)];
        _typeClass = NSClassFromString(_code);
        _numberType = (_typeClass == [NSNumber class] || [_typeClass isSubclassOfClass:[NSNumber class]]);
        // 判断是否是模型类
        _fromFoundation = [NSObject isClassFromFoundation:_typeClass];
    }
    
    // 是否为数字类型
    NSString *lowerCode = code.lowercaseString;
    NSArray *numberTypes = @[JFPropertyTypeInt, JFPropertyTypeShort, JFPropertyTypeBOOL1, JFPropertyTypeBOOL2, JFPropertyTypeFloat, JFPropertyTypeDouble, JFPropertyTypeLong, JFPropertyTypeChar];
    if ([numberTypes containsObject:lowerCode]) {
        _numberType = YES;
        
        if ([lowerCode isEqualToString:JFPropertyTypeBOOL1]
            || [lowerCode isEqualToString:JFPropertyTypeBOOL2]) {
            _boolType = YES;
        }
    }
}

定义一个常量类来描述这些code

/**
 *  成员变量类型(属性类型)
 */
NSString *const JFPropertyTypeInt = @"i";
NSString *const JFPropertyTypeShort = @"s";
NSString *const JFPropertyTypeFloat = @"f";
NSString *const JFPropertyTypeDouble = @"d";
NSString *const JFPropertyTypeLong = @"q";
NSString *const JFPropertyTypeChar = @"c";
NSString *const JFPropertyTypeBOOL1 = @"c";
NSString *const JFPropertyTypeBOOL2 = @"b";
NSString *const JFPropertyTypePointer = @"*";

NSString *const JFPropertyTypeIvar = @"^{objc_ivar=}";
NSString *const JFPropertyTypeMethod = @"^{objc_method=}";
NSString *const JFPropertyTypeBlock = @"@?";
NSString *const JFPropertyTypeClass = @"#";
NSString *const JFPropertyTypeSEL = @":";
NSString *const JFPropertyTypeId = @"@";

extern NSString *const JFPropertyTypeInt;
extern NSString *const JFPropertyTypeShort;
extern NSString *const JFPropertyTypeFloat;
extern NSString *const JFPropertyTypeDouble;
extern NSString *const JFPropertyTypeLong;
extern NSString *const JFPropertyTypeLongLong;
extern NSString *const JFPropertyTypeChar;
extern NSString *const JFPropertyTypeBOOL1;
extern NSString *const JFPropertyTypeBOOL2;
extern NSString *const JFPropertyTypePointer;

extern NSString *const JFPropertyTypeIvar;
extern NSString *const JFPropertyTypeMethod;
extern NSString *const JFPropertyTypeBlock;
extern NSString *const JFPropertyTypeClass;
extern NSString *const JFPropertyTypeSEL;
extern NSString *const JFPropertyTypeId;

image.png

现在我们再统一下 目的:

1.拿到模型的属性名,和对应的数据类型. (OK)

2.用该属性名作为键去字典中寻找对应的值.(OK)

3.拿到值后将值转换为属性对应的数据类型.(OK)

4.赋值.

接下来再整理下 目录结构

image.png

如上:整个JFModel的框架已经搭好了

怎么使用:
https://github.com/tubie/JFModel

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,100评论 18 139
  • 1. 世界足够大 但我愿意再小一些—— 在寂静里舞蹈 这是你从未有过的自在和欢愉 2. 你的疼痛,你的伤口 对不起...
    忧伤没有伤口阅读 88评论 3 4
  • 韩国国家公园管理公团 2005年,韩国留学生韩相一在《中国园林》发表文章,详细地介绍了韩国国家公园形成的历史和演变...
    wwwWo阅读 948评论 0 2
  • 今天下班到图书馆转悠了一圈,走到基金、期货书籍的位置不自觉地停下来翻了一阵子,五花八门的投资书籍,其实对于我这个对...
    A00小浅阅读 225评论 0 2
  • 本文目录: 简介 快速通道 翻译--image库 1. 简介 Rust这门编程语言魅力非常。奈何在国内风名不显,对...
    被叫做逸轩的可儿阅读 3,717评论 0 4