真的好久好久没写点东西了,先对这段时间的总结吧。
首先对技术的热忱从未消减,但是总是什么都想学,什么都是半途而废,比如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:
我们打印出来这个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
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:
我们取出每个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;
现在我们再统一下 目的:
1.拿到模型的属性名,和对应的数据类型. (OK)
2.用该属性名作为键去字典中寻找对应的值.(OK)
3.拿到值后将值转换为属性对应的数据类型.(OK)
4.赋值.
接下来再整理下 目录结构
如上:整个JFModel的框架已经搭好了