YYModel源码解析

文章目录

  • YYModel源码解析
    • 前言
    • YYModel性能优势
    • YYModel简介
      • YYClassInfo解析
        • YYClassIvarInfo && objc_ivar
        • YYClassMethodInfo && objc_method
        • YYClassPropertyInfo && property_t
        • YYClassInfo && objc_class
      • YYClassInfo的初始化细节
      • NSObject+YYModel 探究
        • 类型编码解析
        • 数据结构的定义
          • _YYModelPropertyMeta
          • _YYModelMeta
      • JSON->Model
        • JSON->Dictionary
        • NSDictionary->Model
      • Model->JSON
    • 总结

前言

最近学习一些有关于第三方库源码的内容,把YYModel和JSONModel这两个第三方库的内容看完

YYModel性能优势

YYModel 是一个非常轻量级的 JSON 模型自动转换库,代码风格良好且思路清晰,可以从源码中看到作者对 Runtime 深厚的理解。难能可贵的是 YYModel 在其轻量级的代码下还保留着自动类型转换,类型安全,无侵入等特性,并且具有接近手写解析代码的超高性能。

img

YYModel简介

img

这里其实我们可以看出YYModel的结构其实是比较简单的,其实只有YYClassInfoNSObject+YYModel两个模块

  • 前者将Runtime层级的一些结构体封装到NSObject层以便调用
  • 后者负责提供方便调用的接口以及实现具体的模型转换逻辑

YYClassInfo解析

img

这里我们看一张图来对比理解这里的YYClassInfoRuntime层级的一些结构体的关系:

YYClassInfoRuntime
YYClassIvarInfoobjc_ivar
YYClassMethodInfoobjc_method
YYClassPropertyInfoproperty_t
YYClassInfoobjc_class

下面我会一一对比两者直接的关系:

YYClassIvarInfo && objc_ivar
@interface YYClassIvarInfo : NSObject
@property (nonatomic, assign, readonly) Ivar ivar;              ///< ivar opaque struct 变量的结构体
@property (nonatomic, strong, readonly) NSString *name;         ///< Ivar's name 变量名称
@property (nonatomic, assign, readonly) ptrdiff_t offset;       ///< Ivar's offset 变量的偏移量
@property (nonatomic, strong, readonly) NSString *typeEncoding; ///< Ivar's type encoding 变量类型编码
@property (nonatomic, assign, readonly) YYEncodingType type;    ///< Ivar's type 变量类型,通过YYEncodingGetType方法从类型编码中取得/**Creates and returns an ivar info object.@param ivar ivar opaque struct@return A new object, or nil if an error occurs.*/
- (instancetype)initWithIvar:(Ivar)ivar;
@end//对应的objc_var
struct objc_ivar {char * _Nullable ivar_name OBJC2_UNAVAILABLE; // 变量名称char * _Nullable ivar_type OBJC2_UNAVAILABLE; // 变量类型int ivar_offset OBJC2_UNAVAILABLE; // 变量偏移量
#ifdef __LP64__ // 如果已定义 __LP64__ 则表示正在构建 64 位目标int space OBJC2_UNAVAILABLE; // 变量空间
#endif
}

这里我们发现这里的NSString类型都被strong来修饰,因为所有的字符串都是通过Runtime方法拿到的const char *之后通过stringWithUTF8String方法转化成NSString的,这里使用strong做一次单纯的强引用在性能上会比copy高

YYClassMethodInfo && objc_method
@interface YYClassMethodInfo : NSObject
@property (nonatomic, assign, readonly) Method method;                  ///< method opaque struct // 方法
@property (nonatomic, strong, readonly) NSString *name;                 ///< method name // 方法名
@property (nonatomic, assign, readonly) SEL sel;                        ///< method's selector //sel 
@property (nonatomic, assign, readonly) IMP imp;                        ///< method's implementation //IMP指针
@property (nonatomic, strong, readonly) NSString *typeEncoding;         ///< method's parameter and return types // 方法参数和方法类型
@property (nonatomic, strong, readonly) NSString *returnTypeEncoding;   ///< return value's type // 返回值类型编码
@property (nullable, nonatomic, strong, readonly) NSArray<NSString *> *argumentTypeEncodings; ///< array of arguments' type // 参数类型编码
//源码
struct objc_method {SEL _Nonnull method_name OBJC2_UNAVAILABLE; // 方法名称char * _Nullable method_types OBJC2_UNAVAILABLE; // 方法类型IMP _Nonnull method_imp OBJC2_UNAVAILABLE; // 方法实现(函数指针)
}

sel 可以理解为方法选择器,也就是映射到方法的C字符串

imp 也就是指向方法具体实现逻辑的函数指针

YYClassPropertyInfo && property_t
@interface YYClassPropertyInfo : NSObject
@property (nonatomic, assign, readonly) objc_property_t property; ///< 属性
@property (nonatomic, strong, readonly) NSString *name; ///< 属性名称
@property (nonatomic, assign, readonly) YYEncodingType type; ///< 属性类型
@property (nonatomic, strong, readonly) NSString *typeEncoding; ///< 属性类型编码
@property (nonatomic, strong, readonly) NSString *ivarName; ///< 变量名称
@property (nullable, nonatomic, assign, readonly) Class cls; ///< 类型
@property (nullable, nonatomic, strong, readonly) NSArray<NSString *> *protocols; ///< 属性相关协议
@property (nonatomic, assign, readonly) SEL getter; ///< getter 方法选择器
@property (nonatomic, assign, readonly) SEL setter; ///< setter 方法选择器- (instancetype)initWithProperty:(objc_property_t)property;
@endstruct property_t {const char *name; // 名称const char *attributes; // 修饰
};

可以理解成对于YYClassPropertyInfo是作者对于property_t封装,注意一下对于getter和setter方法的封装

//先尝试获取属性的getter和setter方法
case 'G': {type |= YYEncodingTypePropertyCustomGetter;if (attrs[i].value) {_getter = NSSelectorFromString([NSString stringWithUTF8String:attrs[i].value]);}} break;case 'S': {type |= YYEncodingTypePropertyCustomSetter;if (attrs[i].value) {_setter = NSSelectorFromString([NSString stringWithUTF8String:attrs[i].value]);}} // break; commented for code coverage in next line
// 如果没有则按照标准规则自己造
if (!_getter) {_getter = NSSelectorFromString(_name);}if (!_setter) {_setter = NSSelectorFromString([NSString stringWithFormat:@"set%@%@:", [_name substringToIndex:1].uppercaseString, [_name substringFromIndex:1]]);}
YYClassInfo && objc_class

最后YYModel还封装了objc_class,objc_class在Runtime中表示一个OC类.

@interface YYClassInfo : NSObject
@property (nonatomic, assign, readonly) Class cls; ///< 类
@property (nullable, nonatomic, assign, readonly) Class superCls; ///< 超类
@property (nullable, nonatomic, assign, readonly) Class metaCls;  ///< 元类
@property (nonatomic, readonly) BOOL isMeta; ///< 元类标识,自身是否为元类
@property (nonatomic, strong, readonly) NSString *name; ///< 类名称
@property (nullable, nonatomic, strong, readonly) YYClassInfo *superClassInfo; ///< 父类(超类)信息
@property (nullable, nonatomic, strong, readonly) NSDictionary<NSString *, YYClassIvarInfo *> *ivarInfos; ///< 变量信息
@property (nullable, nonatomic, strong, readonly) NSDictionary<NSString *, YYClassMethodInfo *> *methodInfos; ///< 方法信息
@property (nullable, nonatomic, strong, readonly) NSDictionary<NSString *, YYClassPropertyInfo *> *propertyInfos; ///< 属性信息- (void)setNeedUpdate;
- (BOOL)needUpdate;+ (nullable instancetype)classInfoWithClass:(Class)cls;
+ (nullable instancetype)classInfoWithClassName:(NSString *)className;@end// 源码部分  
typedef struct objc_class *Class;// runtime.h
struct objc_class {Class _Nonnull isa OBJC_ISA_AVAILABILITY; // isa 指针#if !__OBJC2__Class _Nullable super_class OBJC2_UNAVAILABLE; // 父类(超类)指针const char * _Nonnull name OBJC2_UNAVAILABLE; // 类名long version OBJC2_UNAVAILABLE; // 版本long info OBJC2_UNAVAILABLE; // 信息long instance_size OBJC2_UNAVAILABLE; // 初始尺寸struct objc_ivar_list * _Nullable ivars OBJC2_UNAVAILABLE; // 变量列表struct objc_method_list * _Nullable * _Nullable methodLists OBJC2_UNAVAILABLE; // 方法列表struct objc_cache * _Nonnull cache OBJC2_UNAVAILABLE; // 缓存struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE; // 协议列表
#endif} OBJC2_UNAVAILABLE;

这里可以看到他对于每一个类对象里面所有内容进行封装.

上面简单介绍了一下这几个核心结构体和Runtime的底层的对于关系,以及一个处理

YYClassInfo的初始化细节

我们这里是把objc_class在外层封装成一个YYClassInfo,这里就简单看一下这个初始化函数

+ (instancetype)classInfoWithClass:(Class)cls {// 判空入参if (!cls) return nil;// 单例缓存 classCache 与 metaCache,对应缓存类和元类static CFMutableDictionaryRef classCache;static CFMutableDictionaryRef metaCache;static dispatch_once_t onceToken;static dispatch_semaphore_t lock;dispatch_once(&onceToken, ^{classCache = CFDictionaryCreateMutable(CFAllocatorGetDefault(), 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);metaCache = CFDictionaryCreateMutable(CFAllocatorGetDefault(), 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);// 这里把 dispatch_semaphore 当做锁来使用(当信号量只有 1 时)lock = dispatch_semaphore_create(1);});// 初始化之前,首先会根据当前 YYClassInfo 是否为元类去对应的单例缓存中查找// 这里使用了上面的 dispatch_semaphore 加锁,保证单例缓存的线程安全 dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER);YYClassInfo *info = CFDictionaryGetValue(class_isMetaClass(cls) ? metaCache : classCache, (__bridge const void *)(cls));// 如果找到了,且找到的信息需要更新的话则执行更新操作if (info && info->_needUpdate) { // 这个如果发生类信息发生变化,就进行一次加载[info _update];}dispatch_semaphore_signal(lock);// 如果没找到,才会去老实初始化if (!info) {info = [[YYClassInfo alloc] initWithClass:cls];if (info) { // 初始化成功// 线程安全dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER);// 根据初始化信息选择向对应的类/元类缓存注入信息,key = cls,value = infoCFDictionarySetValue(info.isMeta ? metaCache : classCache, (__bridge const void *)(cls), (__bridge const void *)(info));dispatch_semaphore_signal(lock);}}return info;
}
  • 创建单利缓存,类缓存和元类缓存
  • 使用信号量(dispatch_semaphore)作为锁保证缓存线程安全
  • 初始化前先去缓存中查找是否已经象缓存中注册当前要初始化的YYClassInfo
  • 如果查找到缓存对象,则判断缓存对象是否需要更新并执行相关操作
  • 如果缓存中为找到缓存对象则初始化
  • 初始化完成后向缓存中注册YYClassInfo实例

NSObject+YYModel 探究

这里是利用YYClassInfo层级封装好的类切实的执行JSON模型的有一个转换逻辑,并且提供类一个无侵入性的接口,主要分成了四个模块实现对于JSON的逻辑转换

  • 类型编码解析
  • 数据结构定义
  • 递归模型转换
  • 接口相关代码
类型编码解析

类型编码解析代码主要集中在 NSObject+YYModel.m 的上面部分,涉及到 YYEncodingNSType 枚举的定义,配套 YYClassGetNSType 函数将 NS 类型转为 YYEncodingNSType 还有 YYEncodingTypeIsCNumber 函数判断类型是否可以直接转为 C 语言数值类型的函数。

typedef NS_ENUM (NSUInteger, YYEncodingNSType) {YYEncodingTypeNSUnknown = 0,YYEncodingTypeNSString,YYEncodingTypeNSMutableString,YYEncodingTypeNSValue,YYEncodingTypeNSNumber,YYEncodingTypeNSDecimalNumber,YYEncodingTypeNSData,YYEncodingTypeNSMutableData,YYEncodingTypeNSDate,YYEncodingTypeNSURL,YYEncodingTypeNSArray,YYEncodingTypeNSMutableArray,YYEncodingTypeNSDictionary,YYEncodingTypeNSMutableDictionary,YYEncodingTypeNSSet,YYEncodingTypeNSMutableSet,
};static force_inline YYEncodingNSType YYClassGetNSType(Class cls) {if (!cls) return YYEncodingTypeNSUnknown;if ([cls isSubclassOfClass:[NSMutableString class]]) return YYEncodingTypeNSMutableString;if ([cls isSubclassOfClass:[NSString class]]) return YYEncodingTypeNSString;if ([cls isSubclassOfClass:[NSDecimalNumber class]]) return YYEncodingTypeNSDecimalNumber;if ([cls isSubclassOfClass:[NSNumber class]]) return YYEncodingTypeNSNumber;if ([cls isSubclassOfClass:[NSValue class]]) return YYEncodingTypeNSValue;if ([cls isSubclassOfClass:[NSMutableData class]]) return YYEncodingTypeNSMutableData;if ([cls isSubclassOfClass:[NSData class]]) return YYEncodingTypeNSData;if ([cls isSubclassOfClass:[NSDate class]]) return YYEncodingTypeNSDate;if ([cls isSubclassOfClass:[NSURL class]]) return YYEncodingTypeNSURL;if ([cls isSubclassOfClass:[NSMutableArray class]]) return YYEncodingTypeNSMutableArray;if ([cls isSubclassOfClass:[NSArray class]]) return YYEncodingTypeNSArray;if ([cls isSubclassOfClass:[NSMutableDictionary class]]) return YYEncodingTypeNSMutableDictionary;if ([cls isSubclassOfClass:[NSDictionary class]]) return YYEncodingTypeNSDictionary;if ([cls isSubclassOfClass:[NSMutableSet class]]) return YYEncodingTypeNSMutableSet;if ([cls isSubclassOfClass:[NSSet class]]) return YYEncodingTypeNSSet;return YYEncodingTypeNSUnknown;
}
//这两个部分的内容实现将cls快速转化成枚举值,方便后面对于不同类型进行一个处理
//认识一下inline这个内容:主要就是编译器将函数体直接插入调用出,而不是生成函数调用,减小函数调用的时候的开销,就相当于减少了一个函数栈帧插入的一个过程

这里我们看一下有关于将YYNSBlockClass指向了NSBlock类,实现过程如下:

static force_inline Class YYNSBlockClass() {static Class cls;static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{void (^block)(void) = ^{}; // 创建一个空的Blockcls = ((NSObject *)block).class; //获得指针while (class_getSuperclass(cls) != [NSObject class]) { // 向上查找cls = class_getSuperclass(cls);}});return cls; // current is "NSBlock"
}
// block的类结构大致如下:
__NSGlobalBlock__↓
__NSBlock__ // 到这一层就不再查找,退出,然后给他赋值给我们的YYNSBlockClass执行NSBlock↓
NSObject
数据结构的定义

NSObject+YYModel 中重新定义了两个类,通过它们来使用 YYClassInfo 中的封装。

NSObject+YYModelYYClassInfo
_YYModelPropertyMetaYYClassPropertyInfo
_YYModelMetaYYClassInfo
_YYModelPropertyMeta

这里是一个模型对象中的属性信息,它包含YYClassPropertyInfo

@interface _YYModelPropertyMeta : NSObject {@packageNSString *_name;             ///< property's name 属性名称YYEncodingType _type;        ///< property's type 属性类型YYEncodingNSType _nsType;    ///< property's Foundation type 框架中的类型BOOL _isCNumber;             ///< is c number type 是否为CNumber, 也就是判断是不是一个基本的类型Class _cls;                  ///< property's class, or nil 属性类Class _genericCls;           ///< container's generic class, or nil if threr's no generic class 属性包含的泛型类型,没有则为 nilSEL _getter;                 ///< getter, or nil if the instances cannot respond getter方法SEL _setter;                 ///< setter, or nil if the instances cannot respond setter方法BOOL _isKVCCompatible;       ///< YES if it can access with key-value coding 如果可以使用KVC就可以返回YESBOOL _isStructAvailableForKeyedArchiver; ///< YES if the struct can encoded with keyed archiver/unarchiver // 如果可以使用 archiver/unarchiver 归/解档则返回 YESBOOL _hasCustomClassFromDictionary; ///< class/generic class implements +modelCustomClassForDictionary:// 类/泛型自定义类型,例如需要在数组中实现不同类型的转换需要用到/*property->key:       _mappedToKey:key     _mappedToKeyPath:nil            _mappedToKeyArray:nilproperty->keyPath:   _mappedToKey:keyPath _mappedToKeyPath:keyPath(array) _mappedToKeyArray:nilproperty->keys:      _mappedToKey:keys[0] _mappedToKeyPath:nil/keyPath    _mappedToKeyArray:keys(array)*/NSString *_mappedToKey;      ///< the key mapped to 映射keyNSArray *_mappedToKeyPath;   ///< the key path mapped to (nil if the name is not key path) 映射keyPath则返回nilNSArray *_mappedToKeyArray;  ///< the key(NSString) or keyPath(NSArray) array (nil if not mapped to multiple keys) key或者keyPath数组,如果没有映射多个key的话则返回nilYYClassPropertyInfo *_info;  ///< property's info 属性信息_YYModelPropertyMeta *_next; ///< next meta if there are multiple properties mapped to the same key. //如果有多个属性映射到同一个key 则执行下一个模型属性元
}
_YYModelMeta

这里表示的是模型对象中的属性信息:

@interface _YYModelMeta : NSObject {@packageYYClassInfo *_classInfo; /// Key:mapped key and key path, Value:_YYModelPropertyMeta.NSDictionary *_mapper; // 当前字典的KEY 与KeyPath Value:_YYModelPropertyMeta的内容/// Array<_YYModelPropertyMeta>, all property meta of this model.NSArray *_allPropertyMetas; // 当前Model所有的 _YYModelPropertyMeta/// Array<_YYModelPropertyMeta>, property meta which is mapped to a key path.NSArray *_keyPathPropertyMetas; // 被映射到 keyPath 的 _YYModelPropertyMeta 数组/// Array<_YYModelPropertyMeta>, property meta which is mapped to multi keys.NSArray *_multiKeysPropertyMetas; // 被映射到 key 的 _YYModelPropertyMeta 数组/// The number of mapped key (and key path), same to _mapper.count.NSUInteger _keyMappedCount;  // key 与keyPath的数量/// Model class type.YYEncodingNSType _nsType; // class的类型BOOL _hasCustomWillTransformFromDictionary;BOOL _hasCustomTransformFromDictionary;BOOL _hasCustomTransformToDictionary;BOOL _hasCustomClassFromDictionary;
}

JSON->Model

这里我们从转化的起点开始学习

+ (instancetype)yy_modelWithJSON:(id)json {// 将 json 转为字典 dicNSDictionary *dic = [self _yy_dictionaryWithJSON:json];// 将json转化为Modelreturn [self yy_modelWithDictionary:dic];
}

这里我们把这个步骤转化成了两个步骤:

img

JSON->Dictionary

这里我们在看一下有关于yy_dictionaryWithJSON

+ (NSDictionary *)_yy_dictionaryWithJSON:(id)json {//入参判空if (!json || json == (id)kCFNull) return nil;NSDictionary *dic = nil;NSData *jsonData = nil;if ([json isKindOfClass:[NSDictionary class]]) {// 如果是 NSDictionary 类则直接赋值dic = json;} else if ([json isKindOfClass:[NSString class]]) {// 如果是 NSString 类则用 UTF-8 编码转 NSDatajsonData = [(NSString *)json dataUsingEncoding : NSUTF8StringEncoding];} else if ([json isKindOfClass:[NSData class]]) {//如果是data直接赋值jsonData = json;}if (jsonData) {// 利用 NSJSONSerialization 方法将 jsonData 转为 dicdic = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:NULL];if (![dic isKindOfClass:[NSDictionary class]]) dic = nil;  // 判断转换结果 }return dic;
}

其中 kCFNull 是 CoreFoundation 中 CFNull 的单例对象。如同 Foundation 框架中的 NSNull 一样,CFNull 是用来表示集合对象中的空值(不允许为 NULL)。CFNull 对象既不允许被创建也不允许被销毁,而是通过定义一个 CFNull 常量,即 kCFNull,在需要空值时使用。

NSDictionary->Model

这里我们再看第二个步骤:

+ (instancetype)yy_modelWithDictionary:(NSDictionary *)dictionary {if (!dictionary || dictionary == (id)kCFNull) return nil;if (![dictionary isKindOfClass:[NSDictionary class]]) return nil;Class cls = [self class];// 使用当前类生成一个 _YYModelMeta 模型元类_YYModelMeta *modelMeta = [_YYModelMeta metaWithClass:cls];// 这里 _hasCustomClassFromDictionary 用于标识是否需要自定义返回类if (modelMeta->_hasCustomClassFromDictionary) {cls = [cls modelCustomClassForDictionary:dictionary] ?: cls;}// 调用 yy_modelSetWithDictionary 为新建的类实例 one 赋值,赋值成功则返回 oneNSObject *one = [cls new];if ([one yy_modelSetWithDictionary:dictionary]) return one;return nil;
}

这里之后再进入下面这个方法yy_modelSetWithDictionary

这个方法可以说是模型转化的核心代码:

- (BOOL)yy_modelSetWithDictionary:(NSDictionary *)dic {if (!dic || dic == (id)kCFNull) return NO;if (![dic isKindOfClass:[NSDictionary class]]) return NO;_YYModelMeta *modelMeta = [_YYModelMeta metaWithClass:object_getClass(self)];//如果key没有就返回NOif (modelMeta->_keyMappedCount == 0) return NO;//是否有 modelCustomWillTransformFromDictionary 接口if (modelMeta->_hasCustomWillTransformFromDictionary) {dic = [((id<YYModel>)self) modelCustomWillTransformFromDictionary:dic];if (![dic isKindOfClass:[NSDictionary class]]) return NO;}//设置上下文内容ModelSetContext context = {0};context.modelMeta = (__bridge void *)(modelMeta);context.model = (__bridge void *)(self);context.dictionary = (__bridge void *)(dic);// 判断模型元键值映射数量与 JSON 所得字典的数量关系, 将dictionary的数据填充到我们的Model层上面if (modelMeta->_keyMappedCount >= CFDictionaryGetCount((CFDictionaryRef)dic)) {// 一般情况下他们的数量相等// 特殊情况比如有的属性元会映射字典中的多个 key// 为字典中的每个键值对调用 ModelSetWithDictionaryFunction// 这句话是核心代码,一般情况下就是靠 ModelSetWithDictionaryFunction 通过字典设置模型CFDictionaryApplyFunction((CFDictionaryRef)dic, ModelSetWithDictionaryFunction, &context);// 处理keyPath的属性if (modelMeta->_keyPathPropertyMetas) {//对每一个keypath调用对应方法CFArrayApplyFunction((CFArrayRef)modelMeta->_keyPathPropertyMetas,CFRangeMake(0, CFArrayGetCount((CFArrayRef)modelMeta->_keyPathPropertyMetas)),ModelSetWithPropertyMetaArrayFunction,&context);}//处理映射多个 key 的属性元if (modelMeta->_multiKeysPropertyMetas) {CFArrayApplyFunction((CFArrayRef)modelMeta->_multiKeysPropertyMetas,CFRangeMake(0, CFArrayGetCount((CFArrayRef)modelMeta->_multiKeysPropertyMetas)),ModelSetWithPropertyMetaArrayFunction,&context);}} else {//处理单个key的方法CFArrayApplyFunction((CFArrayRef)modelMeta->_allPropertyMetas,CFRangeMake(0, modelMeta->_keyMappedCount),ModelSetWithPropertyMetaArrayFunction,&context);}if (modelMeta->_hasCustomTransformFromDictionary) {return [((id<YYModel>)self) modelCustomTransformFromDictionary:dic];}return YES;
}

这里主要分成了下面几个步骤:

  • 入参校验
  • 初始化模型元以及映射表校验
  • 初始化模型设置上下文 ModelSetContext
  • 为字典中的每个键值对调用 ModelSetWithDictionaryFunction
  • 检验转换结果

上下文的结构体如下图:

typedef struct {void *modelMeta;  ///< 模型元void *model;      ///< 模型实例,指向输出的模型void *dictionary; ///< 待转换字典
} ModelSetContext;

下面在看一下这个方法:ModelSetWithDictionaryFunction:这一步可以理解为设置Model的逻辑

static void ModelSetWithDictionaryFunction(const void *_key, const void *_value, void *_context) {//获得入参上下文ModelSetContext *context = _context;//获得对应的模型元__unsafe_unretained _YYModelMeta *meta = (__bridge _YYModelMeta *)(context->modelMeta);//获得对应的属性元__unsafe_unretained _YYModelPropertyMeta *propertyMeta = [meta->_mapper objectForKey:(__bridge id)(_key)];//获得对应的一个未处理的model__unsafe_unretained id model = (__bridge id)(context->model);// 遍历 propertyMeta,直到 propertyMeta->_next == nilwhile (propertyMeta) {if (propertyMeta->_setter) { //如果有setter方法就采用setter,则调用 ModelSetValueForProperty 赋值ModelSetValueForProperty(model, (__bridge __unsafe_unretained id)_value, propertyMeta);}propertyMeta = propertyMeta->_next;};
}

这个方法就是通过获取对应的一个属性元,找到 setter 不为空的属性元通过 ModelSetValueForProperty 方法赋值。

ModelSetValueForProperty 函数是为模型中的属性赋值的实现方法,也是整个 YYModel 的核心代码

static void ModelSetValueForProperty(__unsafe_unretained id model,__unsafe_unretained id value,__unsafe_unretained _YYModelPropertyMeta *meta) {if (meta->_isCNumber) {NSNumber *num = YYNSNumberCreateFromID(value);ModelSetNumberToProperty(model, num, meta);if (num) [num class]; // hold the number} else if (meta->_nsType) {if (value == (id)kCFNull) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, (id)nil);} else {switch (meta->_nsType) {case YYEncodingTypeNSString:case YYEncodingTypeNSMutableString: {if ([value isKindOfClass:[NSString class]]) {if (meta->_nsType == YYEncodingTypeNSString) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, value);} else {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, ((NSString *)value).mutableCopy);}} else if ([value isKindOfClass:[NSNumber class]]) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model,meta->_setter,(meta->_nsType == YYEncodingTypeNSString) ?((NSNumber *)value).stringValue :((NSNumber *)value).stringValue.mutableCopy);} else if ([value isKindOfClass:[NSData class]]) {NSMutableString *string = [[NSMutableString alloc] initWithData:value encoding:NSUTF8StringEncoding];((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, string);} else if ([value isKindOfClass:[NSURL class]]) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model,meta->_setter,(meta->_nsType == YYEncodingTypeNSString) ?((NSURL *)value).absoluteString :((NSURL *)value).absoluteString.mutableCopy);} else if ([value isKindOfClass:[NSAttributedString class]]) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model,meta->_setter,(meta->_nsType == YYEncodingTypeNSString) ?((NSAttributedString *)value).string :((NSAttributedString *)value).string.mutableCopy);}} break;case YYEncodingTypeNSValue:case YYEncodingTypeNSNumber:case YYEncodingTypeNSDecimalNumber: {if (meta->_nsType == YYEncodingTypeNSNumber) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, YYNSNumberCreateFromID(value));} else if (meta->_nsType == YYEncodingTypeNSDecimalNumber) {if ([value isKindOfClass:[NSDecimalNumber class]]) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, value);} else if ([value isKindOfClass:[NSNumber class]]) {NSDecimalNumber *decNum = [NSDecimalNumber decimalNumberWithDecimal:[((NSNumber *)value) decimalValue]];((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, decNum);} else if ([value isKindOfClass:[NSString class]]) {NSDecimalNumber *decNum = [NSDecimalNumber decimalNumberWithString:value];NSDecimal dec = decNum.decimalValue;if (dec._length == 0 && dec._isNegative) {decNum = nil; // NaN}((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, decNum);}} else { // YYEncodingTypeNSValueif ([value isKindOfClass:[NSValue class]]) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, value);}}} break;case YYEncodingTypeNSData:case YYEncodingTypeNSMutableData: {if ([value isKindOfClass:[NSData class]]) {if (meta->_nsType == YYEncodingTypeNSData) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, value);} else {NSMutableData *data = ((NSData *)value).mutableCopy;((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, data);}} else if ([value isKindOfClass:[NSString class]]) {NSData *data = [(NSString *)value dataUsingEncoding:NSUTF8StringEncoding];if (meta->_nsType == YYEncodingTypeNSMutableData) {data = ((NSData *)data).mutableCopy;}((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, data);}} break;case YYEncodingTypeNSDate: {if ([value isKindOfClass:[NSDate class]]) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, value);} else if ([value isKindOfClass:[NSString class]]) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, YYNSDateFromString(value));}} break;case YYEncodingTypeNSURL: {if ([value isKindOfClass:[NSURL class]]) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, value);} else if ([value isKindOfClass:[NSString class]]) {NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];NSString *str = [value stringByTrimmingCharactersInSet:set];if (str.length == 0) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, nil);} else {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, [[NSURL alloc] initWithString:str]);}}} break;case YYEncodingTypeNSArray:case YYEncodingTypeNSMutableArray: {if (meta->_genericCls) {NSArray *valueArr = nil;if ([value isKindOfClass:[NSArray class]]) valueArr = value;else if ([value isKindOfClass:[NSSet class]]) valueArr = ((NSSet *)value).allObjects;if (valueArr) {NSMutableArray *objectArr = [NSMutableArray new];for (id one in valueArr) {if ([one isKindOfClass:meta->_genericCls]) {[objectArr addObject:one];} else if ([one isKindOfClass:[NSDictionary class]]) {Class cls = meta->_genericCls;if (meta->_hasCustomClassFromDictionary) {cls = [cls modelCustomClassForDictionary:one];if (!cls) cls = meta->_genericCls; // for xcode code coverage}NSObject *newOne = [cls new];[newOne yy_modelSetWithDictionary:one];if (newOne) [objectArr addObject:newOne];}}((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, objectArr);}} else {if ([value isKindOfClass:[NSArray class]]) {if (meta->_nsType == YYEncodingTypeNSArray) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, value);} else {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model,meta->_setter,((NSArray *)value).mutableCopy);}} else if ([value isKindOfClass:[NSSet class]]) {if (meta->_nsType == YYEncodingTypeNSArray) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, ((NSSet *)value).allObjects);} else {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model,meta->_setter,((NSSet *)value).allObjects.mutableCopy);}}}} break;case YYEncodingTypeNSDictionary:case YYEncodingTypeNSMutableDictionary: {if ([value isKindOfClass:[NSDictionary class]]) {if (meta->_genericCls) {NSMutableDictionary *dic = [NSMutableDictionary new];[((NSDictionary *)value) enumerateKeysAndObjectsUsingBlock:^(NSString *oneKey, id oneValue, BOOL *stop) {if ([oneValue isKindOfClass:[NSDictionary class]]) {Class cls = meta->_genericCls;if (meta->_hasCustomClassFromDictionary) {cls = [cls modelCustomClassForDictionary:oneValue];if (!cls) cls = meta->_genericCls; // for xcode code coverage}NSObject *newOne = [cls new];[newOne yy_modelSetWithDictionary:(id)oneValue];if (newOne) dic[oneKey] = newOne;}}];((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, dic);} else {if (meta->_nsType == YYEncodingTypeNSDictionary) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, value);} else {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model,meta->_setter,((NSDictionary *)value).mutableCopy);}}}} break;case YYEncodingTypeNSSet:case YYEncodingTypeNSMutableSet: {NSSet *valueSet = nil;if ([value isKindOfClass:[NSArray class]]) valueSet = [NSMutableSet setWithArray:value];else if ([value isKindOfClass:[NSSet class]]) valueSet = ((NSSet *)value);if (meta->_genericCls) {NSMutableSet *set = [NSMutableSet new];for (id one in valueSet) {if ([one isKindOfClass:meta->_genericCls]) {[set addObject:one];} else if ([one isKindOfClass:[NSDictionary class]]) {Class cls = meta->_genericCls;if (meta->_hasCustomClassFromDictionary) {cls = [cls modelCustomClassForDictionary:one];if (!cls) cls = meta->_genericCls; // for xcode code coverage}NSObject *newOne = [cls new];[newOne yy_modelSetWithDictionary:one];if (newOne) [set addObject:newOne];}}((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, set);} else {if (meta->_nsType == YYEncodingTypeNSSet) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, valueSet);} else {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model,meta->_setter,((NSSet *)valueSet).mutableCopy);}}} // break; commented for code coverage in next linedefault: break;}}} else {BOOL isNull = (value == (id)kCFNull);switch (meta->_type & YYEncodingTypeMask) {case YYEncodingTypeObject: {if (isNull) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, (id)nil);} else if ([value isKindOfClass:meta->_cls] || !meta->_cls) {((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, (id)value);} else if ([value isKindOfClass:[NSDictionary class]]) {NSObject *one = nil;if (meta->_getter) {one = ((id (*)(id, SEL))(void *) objc_msgSend)((id)model, meta->_getter);}if (one) {[one yy_modelSetWithDictionary:value];} else {Class cls = meta->_cls;if (meta->_hasCustomClassFromDictionary) {cls = [cls modelCustomClassForDictionary:value];if (!cls) cls = meta->_genericCls; // for xcode code coverage}one = [cls new];[one yy_modelSetWithDictionary:value];((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, (id)one);}}} break;case YYEncodingTypeClass: {if (isNull) {((void (*)(id, SEL, Class))(void *) objc_msgSend)((id)model, meta->_setter, (Class)NULL);} else {Class cls = nil;if ([value isKindOfClass:[NSString class]]) {cls = NSClassFromString(value);if (cls) {((void (*)(id, SEL, Class))(void *) objc_msgSend)((id)model, meta->_setter, (Class)cls);}} else {cls = object_getClass(value);if (cls) {if (class_isMetaClass(cls)) {((void (*)(id, SEL, Class))(void *) objc_msgSend)((id)model, meta->_setter, (Class)value);}}}}} break;case  YYEncodingTypeSEL: {if (isNull) {((void (*)(id, SEL, SEL))(void *) objc_msgSend)((id)model, meta->_setter, (SEL)NULL);} else if ([value isKindOfClass:[NSString class]]) {SEL sel = NSSelectorFromString(value);if (sel) ((void (*)(id, SEL, SEL))(void *) objc_msgSend)((id)model, meta->_setter, (SEL)sel);}} break;case YYEncodingTypeBlock: {if (isNull) {((void (*)(id, SEL, void (^)()))(void *) objc_msgSend)((id)model, meta->_setter, (void (^)())NULL);} else if ([value isKindOfClass:YYNSBlockClass()]) {((void (*)(id, SEL, void (^)()))(void *) objc_msgSend)((id)model, meta->_setter, (void (^)())value);}} break;case YYEncodingTypeStruct:case YYEncodingTypeUnion:case YYEncodingTypeCArray: {if ([value isKindOfClass:[NSValue class]]) {const char *valueType = ((NSValue *)value).objCType;const char *metaType = meta->_info.typeEncoding.UTF8String;if (valueType && metaType && strcmp(valueType, metaType) == 0) {[model setValue:value forKey:meta->_name];}}} break;case YYEncodingTypePointer:case YYEncodingTypeCString: {if (isNull) {((void (*)(id, SEL, void *))(void *) objc_msgSend)((id)model, meta->_setter, (void *)NULL);} else if ([value isKindOfClass:[NSValue class]]) {NSValue *nsValue = value;if (nsValue.objCType && strcmp(nsValue.objCType, "^v") == 0) {((void (*)(id, SEL, void *))(void *) objc_msgSend)((id)model, meta->_setter, nsValue.pointerValue); // 最后都会采用setter方法来给数据赋值}}} // break; commented for code coverage in next linedefault: break;}}
}

这个函数就是通过判断属性类型来对于model层的数据进行一个赋值操作

  • 根据属性元类型划分代码逻辑
  • 如果属性元是 CNumber 类型,即 int、uint 之类,则使用 ModelSetNumberToProperty 赋值
  • 如果属性元属于 NSType 类型,即 NSString、NSNumber 之类,则根据类型转换中可能涉及到的对应类型做逻辑判断并赋值(可以去上面代码中查看具体实现逻辑)
  • 如果属性元不属于 CNumber 和 NSType,则猜测为 id,Class,SEL,Block,struct、union、char[n],void或 char 类型并且做出相应的转换和赋值

这里最后都是采用有一个我们的属性的setter方法来调用的

这里有一位大佬给出了一个图:

img

Model->JSON

我们有从Model转向JSON那自然也有从JSON转向Model的内容,这里看这个函数:

- (id)yy_modelToJSONObject {/*Apple said:The top level object is an NSArray or NSDictionary.All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.All dictionary keys are instances of NSString.Numbers are not NaN or infinity.*/id jsonObject = ModelToJSONObjectRecursive(self);if ([jsonObject isKindOfClass:[NSArray class]]) return jsonObject;if ([jsonObject isKindOfClass:[NSDictionary class]]) return jsonObject;return nil;
}

这里因为是采用的是NSJSONSerialization来进行一个转化的,将一个对象model转化成JSON数据.他有一个下面的要求

  • 顶级对象是 NSArray 或者 NSDictionary 类型

  • 所有的对象都是 NSString, NSNumber, NSArray, NSDictionary, 或 NSNull 的实例

  • 所有字典中的 key 都是一个 NSString 实例

  • Numbers 是除去无穷大和 NaN 的其他表示

这里我们简单看一下这个函数:这不罗列出详细的内容,简单介绍一下相关内容,这里是对于Model层的每一个元素进行一个递归检查的内容,

// 递归转换模型到 JSON,如果转换异常则返回 nil
static id ModelToJSONObjectRecursive(NSObject *model) {// 判空或者可以直接返回的对象,则直接返回if (!model || model == (id)kCFNull) return model;if ([model isKindOfClass:[NSString class]]) return model;if ([model isKindOfClass:[NSNumber class]]) return model;// 如果 model 从属于 NSDictionaryif ([model isKindOfClass:[NSDictionary class]]) {// 如果可以直接转换为 JSON 数据,则返回if ([NSJSONSerialization isValidJSONObject:model]) return model;NSMutableDictionary *newDic = [NSMutableDictionary new];// 遍历 model 的 key 和 value[((NSDictionary *)model) enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {NSString *stringKey = [key isKindOfClass:[NSString class]] ? key : key.description;if (!stringKey) return;// 递归解析 value id jsonObj = ModelToJSONObjectRecursive(obj);if (!jsonObj) jsonObj = (id)kCFNull;newDic[stringKey] = jsonObj;}];return newDic;}// 如果 model 从属于 NSSetif ([model isKindOfClass:[NSSet class]]) {// 如果能够直接转换 JSON 对象,则直接返回// 否则遍历,按需要递归解析...}if ([model isKindOfClass:[NSArray class]]) {// 如果能够直接转换 JSON 对象,则直接返回// 否则遍历,按需要递归解析...}// 对 NSURL, NSAttributedString, NSDate, NSData 做相应处理if ([model isKindOfClass:[NSURL class]]) return ((NSURL *)model).absoluteString;if ([model isKindOfClass:[NSAttributedString class]]) return ((NSAttributedString *)model).string;if ([model isKindOfClass:[NSDate class]]) return [YYISODateFormatter() stringFromDate:(id)model];if ([model isKindOfClass:[NSData class]]) return nil;// 用 [model class] 初始化一个模型元_YYModelMeta *modelMeta = [_YYModelMeta metaWithClass:[model class]];// 如果映射表为空,则不做解析直接返回 nilif (!modelMeta || modelMeta->_keyMappedCount == 0) return nil;// 性能优化细节,使用 __unsafe_unretained 来避免在下面遍历 block 中直接使用 result 指针造成的不必要 retain 与 release 开销NSMutableDictionary *result = [[NSMutableDictionary alloc] initWithCapacity:64];__unsafe_unretained NSMutableDictionary *dic = result; // 遍历模型元属性映射字典[modelMeta->_mapper enumerateKeysAndObjectsUsingBlock:^(NSString *propertyMappedKey, _YYModelPropertyMeta *propertyMeta, BOOL *stop) {// 如果遍历当前属性元没有 getter 方法,跳过if (!propertyMeta->_getter) return;id value = nil;// 如果属性元属于 CNumber,即其 type 是 int、float、double 之类的if (propertyMeta->_isCNumber) {// 从属性中利用 getter 方法得到对应的值value = ModelCreateNumberFromProperty(model, propertyMeta);} else if (propertyMeta->_nsType) { // 属性元属于 nsType,即 NSString 之类// 利用 getter 方法拿到 valueid v = ((id (*)(id, SEL))(void *) objc_msgSend)((id)model, propertyMeta->_getter);// 对拿到的 value 递归解析value = ModelToJSONObjectRecursive(v);} else {// 根据属性元的 type 做相应处理switch (propertyMeta->_type & YYEncodingTypeMask) {// id,需要递归解析,如果解析失败则返回 nilcase YYEncodingTypeObject: {id v = ((id (*)(id, SEL))(void *) objc_msgSend)((id)model, propertyMeta->_getter);value = ModelToJSONObjectRecursive(v);if (value == (id)kCFNull) value = nil;} break;// Class,转 NSString,返回 Class 名称case YYEncodingTypeClass: {Class v = ((Class (*)(id, SEL))(void *) objc_msgSend)((id)model, propertyMeta->_getter);value = v ? NSStringFromClass(v) : nil;} break;// SEL,转 NSString,返回给定 SEL 的字符串表现形式case YYEncodingTypeSEL: {SEL v = ((SEL (*)(id, SEL))(void *) objc_msgSend)((id)model, propertyMeta->_getter);value = v ? NSStringFromSelector(v) : nil;} break;default: break;}}// 如果 value 还是没能解析,则跳过if (!value) return;// 当前属性元是 KeyPath 映射,即 a.b.c 之类if (propertyMeta->_mappedToKeyPath) {NSMutableDictionary *superDic = dic;NSMutableDictionary *subDic = nil;// _mappedToKeyPath 是 a.b.c 根据 '.' 拆分成的字符串数组,遍历 _mappedToKeyPathfor (NSUInteger i = 0, max = propertyMeta->_mappedToKeyPath.count; i < max; i++) {NSString *key = propertyMeta->_mappedToKeyPath[i];// 遍历到结尾if (i + 1 == max) {// 如果结尾的 key 为 nil,则使用 value 赋值if (!superDic[key]) superDic[key] = value;break;}// 用 subDic 拿到当前 key 对应的值subDic = superDic[key];// 如果 subDic 存在if (subDic) {// 如果 subDic 从属于 NSDictionaryif ([subDic isKindOfClass:[NSDictionary class]]) {// 将 subDic 的 mutable 版本赋值给 superDic[key]subDic = subDic.mutableCopy;superDic[key] = subDic;} else {break;}} else {// 将 NSMutableDictionary 赋值给 superDic[key]// 注意这里使用 subDic 间接赋值是有原因的,原因就在下面subDic = [NSMutableDictionary new];superDic[key] = subDic;}// superDic 指向 subDic,这样在遍历 _mappedToKeyPath 时即可逐层解析// 这就是上面先把 subDic 转为 NSMutableDictionary 的原因superDic = subDic;subDic = nil;}} else {// 如果不是 KeyPath 则检测 dic[propertyMeta->_mappedToKey],如果为 nil 则赋值 valueif (!dic[propertyMeta->_mappedToKey]) {dic[propertyMeta->_mappedToKey] = value;}}}];                         // 忽略,对应 modelCustomTransformToDictionary 接口if (modelMeta->_hasCustomTransformToDictionary) {// 用于在默认的 Model 转 JSON 过程不适合当前 Model 类型时提供自定义额外过程// 也可以用这个方法来验证转换结果BOOL suc = [((id<YYModel>)model) modelCustomTransformToDictionary:dic];if (!suc) return nil;}return result;
}

代码还是有些长,这里笔者简单讲一下这里的内容,简单总结一下上面的代码逻辑:

  • 判断入参,如果满足条件可以直接返回
  • 如果Model从属于NSType,根据不同的类型做逻辑处理
  • 如果上面条件不被满足,则用Model的Class初始化一个模型元_YYModelMeta
  • 判断模型元的映射关系,遍历映射拿到对应的键值存入字典中返回

这里有一个性能优化的细节,用__unsafe_unretained修饰的dic指向我们最后要return的NSMutableDictionary *result.

这里作者大篇幅的采用来unsafe_unretained来处理循环引用的内容,这里就可以减少很多不需要的retainrelease的操作,因为这样可以提高速度,

总结

这里笔者学习YYModel主要是为了认识为什么YYModel有更多的性能优势?

  • 首先YYModel十分轻量级,它将Runtime的底层结构体封装了成了方便调用的结构体,减少来代码逻辑
  • 同时YYModel中频繁调用了Core Foundation框架中的内容,Core Foundation方法在调用的时候远远优与Foundation中的方法
  • 同时在YYModel中经常使用纯C函数,避免了消息发送的开销
  • 还有与JSONModel最有区别的一点就是YYModel中使用setter方法进行了value值的设置而非KVC,这样也提高了性能
  • YYModel采用了分类,实现了对于Model的无入侵性,这点比较重要

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/83703.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/83703.shtml
英文地址,请注明出处:http://en.pswp.cn/pingmian/83703.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

宇树科技更名“股份有限公司”深度解析:机器人企业IPO前奏与资本化路径

从技术落地到资本跃迁&#xff0c;拆解股改背后的上市逻辑与行业启示 核心事件&#xff1a;股改释放的上市信号 2025年5月28日&#xff0c;杭州宇树科技有限公司正式更名“杭州宇树科技股份有限公司”&#xff0c;市场主体类型变更为“股份有限公司”。尽管官方称为常规运营调…

Android Native 内存泄漏检测全解析:从原理到工具的深度实践

引言 Android应用的内存泄漏不仅发生在Java/Kotlin层&#xff0c;Native&#xff08;C/C&#xff09;层的泄漏同样普遍且隐蔽。由于Native内存不受Java虚拟机&#xff08;JVM&#xff09;管理&#xff0c;泄漏的内存无法通过GC自动回收&#xff0c;长期积累会导致应用内存占用…

Vortex GPGPU的github流程跑通与功能模块波形探索(四)

文章目录 前言一、demo的输入文件二、trace_csv三、2个值得注意的点3.1 csv指令表格里面的tmask&#xff1f;3.2 rtlsim和simx的log文件&#xff1f; 总结 前言 跟着前面那篇最后留下的几个问题接着把输出波形文件和csv文件的输入、输出搞明白&#xff01; 一、demo的输入文件…

UnityPSD文件转UI插件Psd2UnityuGUIPro3.4.0u2017.4.2介绍:Unity UI设计的高效助手

UnityPSD文件转UI插件Psd2UnityuGUIPro3.4.0u2017.4.2介绍&#xff1a;Unity UI设计的高效助手 【下载地址】UnityPSD文件转UI插件Psd2UnityuGUIPro3.4.0u2017.4.2介绍 这款开源插件将PSD文件无缝转换为Unity的UI元素&#xff0c;极大提升开发效率。它支持一键转换&#xff0c;…

力扣100题之128. 最长连续序列

方法1 使用了hash 方法思路 使用哈希集合&#xff1a;首先将数组中的所有数字存入一个哈希集合中&#xff0c;这样可以在 O(1) 时间内检查某个数字是否存在。 寻找连续序列&#xff1a;遍历数组中的每一个数字&#xff0c;对于每一个数字&#xff0c; 检查它是否是某个连续序列…

Java爬虫技术详解:原理、实现与优势

一、什么是网络爬虫&#xff1f; 网络爬虫&#xff08;Web Crawler&#xff09;&#xff0c;又称网络蜘蛛或网络机器人&#xff0c;是一种自动化程序&#xff0c;能够按照一定的规则自动浏览和抓取互联网上的信息。爬虫技术是大数据时代获取网络数据的重要手段&#xff0c;广泛…

神经网络与深度学习 网络优化与正则化

1.网络优化存在的难点 &#xff08;1&#xff09;结构差异大&#xff1a;没有通用的优化算法&#xff1b;超参数多 &#xff08;2&#xff09;非凸优化问题&#xff1a;参数初始化&#xff0c;逃离局部最优 &#xff08;3&#xff09;梯度消失&#xff08;爆炸&#xff09; …

【汇编逆向系列】二、函数调用包含单个参数之整型-ECX寄存器,LEA指令

目录 一. 汇编源码 二. 汇编分析 1. ECX寄存器 2. 栈位置计算​ 3. 特殊指令深度解析 三、 汇编转化 一. 汇编源码 single_int_param:0000000000000040: 89 4C 24 08 mov dword ptr [rsp8],ecx0000000000000044: 57 push rdi0000…

Linux进程替换以及exec六大函数运用

文章目录 1.进程替换2.替换过程3.替换函数exec3.1命名解释 4.细说6个exe函数execl函数execvexeclp、execvpexecle、execve 1.进程替换 fork&#xff08;&#xff09;函数在创建子进程后&#xff0c;子进程如果想要执行一个新的程序&#xff0c;就可以使用进程的程序替换来完成…

Selenium操作指南(全)

&#x1f345; 点击文末小卡片&#xff0c;免费获取软件测试全套资料&#xff0c;资料在手&#xff0c;涨薪更快 大家好&#xff0c;今天带大家一起系统的学习下模拟浏览器运行库Selenium&#xff0c;它是一个用于Web自动化测试及爬虫应用的重要工具。 Selenium测试直接运行在…

结构性设计模式之Facade(外观)设计模式

结构性设计模式之Facade&#xff08;外观&#xff09;设计模式 前言&#xff1a; 外观模式&#xff1a;用自己的话理解就是用户看到是一个总体页面&#xff0c;比如xx报名系统页面。里面有历年真题模块、报名模块、教程模块、首页模块… 做了一个各个模块的合并&#xff0c;对…

RabbitMQ实用技巧

RabbitMQ是一个流行的开源消息中间件&#xff0c;广泛用于实现消息传递、任务分发和负载均衡。通过合理使用RabbitMQ的功能&#xff0c;可以显著提升系统的性能、可靠性和可维护性。本文将介绍一些RabbitMQ的实用技巧&#xff0c;包括基础配置、高级功能及常见问题的解决方案。…

Linux(10)——第二个小程序(自制shell)

目录 ​编辑 一、引言与动机 &#x1f4dd;背景 &#x1f4dd;主要内容概括 二、全局数据 三、环境变量的初始化 ✅ 代码实现 四、构造动态提示符 ✅ 打印提示符函数 ✅ 提示符生成函数 ✅获取用户名函数 ✅获取主机名函数 ✅获取当前目录名函数 五、命令的读取与…

环境变量深度解析:从配置到内核的全链路指南

文章目录 一、基础概念与核心作用二、常见环境变量三、操作指南&#xff1a;从查看、修改到调试3.1 快速查询3.2 PATH 原理与配置实践3.2.1 命令执行机制3.2.2 路径管理策略 四、编程接口与内存模型4.1 环境变量的内存结构4.2 C 语言访问方式4.2.1 直接访问&#xff08;main 参…

结合Jenkins、Docker和Kubernetes等主流工具,部署Spring Boot自动化实战指南

基于最佳实践的Spring Boot自动化部署实战指南,结合Jenkins、Docker和Kubernetes等主流工具,提供从环境搭建到生产部署的完整流程: 一、环境准备与工具选型​​ ​​1.基础设施​​ ​​Jenkins服务器​​:安装Jenkins LTS版本,配置JDK(推荐JDK 11+)及Maven/Gradle插…

动态规划---股票问题

1.在推状态转移方程的途中&#xff0c;箭头的起始点表示前一天的状态&#xff0c;箭头的终点是当天的状态 2.当动态规划中涉及到多状态&#xff0c;且状态之间可以相互转换&#xff0c;要画图去分析 1.买卖股票的最佳时机含冷冻期 题目链接&#xff1a;309. 买卖股票的最佳时机…

ObjectMapper 在 Spring 统一响应处理中的作用详解

ObjectMapper 是 Jackson 库的核心类&#xff0c;专门用于处理 JSON 数据的序列化&#xff08;Java 对象 → JSON&#xff09;和反序列化&#xff08;JSON → Java 对象&#xff09;。在你提供的代码中&#xff0c;它解决了字符串响应特殊处理的关键问题。 一、为什么需要 Obj…

总结这几个月来我和AI一起开发并上线第一个应用的使用经验

副标题&#xff1a; 当“手残”前端遇到AI队友&#xff0c;我的音乐小站谱贝诞生记 大家好&#xff0c;我最近干了件“不务正业”的事——**独立开发并上线了一个完整的网站 作为一个前端“手残党”&#xff08;还在努力学习中&#x1f605;&#xff09;&#xff0c;这次能成功…

【大模型:知识图谱】--5.neo4j数据库管理(cypher语法2)

目录 1.节点语法 1.1.CREATE--创建节点 1.2.MATCH--查询节点 1.3.RETURN--返回节点 1.4.WHERE--过滤节点 2.关系语法 2.1.创建关系 2.2.查询关系 3.删除语法 3.1.DELETE 删除 3.2.REMOVE 删除 4.功能补充 4.1.SET &#xff08;添加属性&#xff09; 4.2.NULL 值 …

结构体指针与非指针 问题及解决

问题描述 第一段位于LCD.h和LCD.c中&#xff0c; 定义个一个结构体lcd_params&#xff0c;并直接给与指针名*p_lcd_params; 我发现我在调用这个结构体时&#xff0c;即在LCD.c中&#xff0c;使用指针类型定义的 static p_lcd_params p_array_lcd[LCD_NUM]; static p_lcd_par…