前边3篇介绍了YYClassinfo 文件的组成单元,算是功能的分割,按照业务的设计思想来说,方向应该是相反的
由此引申出我们在设计api的思想其实和项目管理是很类似的----- 一些题外话
1.目的
回到代码,首先应该明确写这个类的目的是什么? 按照正常逻辑,我们需要一个类来获取我们所需要的所有和此类相关的信息
包括(类名,父类,成员变量,方法,属性...)
2.技术调研
调研我们所需要的结果是否能够通过技术手段实现
3.目标分隔,也就是任务分解
需要把整体目标分解成小目标,在本代码中则分割成 三个部分
①获取IVar -----> 具体的实现
②获取Method -----> 具体的实现
③获取Property ------> 具体的实现
好了大概的思想就是上边这些了 ,接下来让我们看看暴露出来的属性是怎么样的,也就是我们所需要的信息是什么
/**
Class information for a class.
*/
@interface YYClassInfo : NSObject
@property (nonatomic, assign, readonly) Class cls; ///< class object
@property (nullable, nonatomic, assign, readonly) Class superCls; ///< super class object
@property (nullable, nonatomic, assign, readonly) Class metaCls; ///< class's meta class object
@property (nonatomic, readonly) BOOL isMeta; ///< whether this class is meta class
@property (nonatomic, strong, readonly) NSString *name; ///< class name
@property (nullable, nonatomic, strong, readonly) YYClassInfo *superClassInfo; ///< super class's class info
@property (nullable, nonatomic, strong, readonly) NSDictionary<NSString *, YYClassIvarInfo *> *ivarInfos; ///< ivars
@property (nullable, nonatomic, strong, readonly) NSDictionary<NSString *, YYClassMethodInfo *> *methodInfos; ///< methods
@property (nullable, nonatomic, strong, readonly) NSDictionary<NSString *, YYClassPropertyInfo *> *propertyInfos; ///< properties /**
If the class is changed (for example: you add a method to this class with
'class_addMethod()'), you should call this method to refresh the class info cache. After called this method, `needUpdate` will returns `YES`, and you should call
'classInfoWithClass' or 'classInfoWithClassName' to get the updated class info.
*/
- (void)setNeedUpdate; /**
If this method returns `YES`, you should stop using this instance and call
`classInfoWithClass` or `classInfoWithClassName` to get the updated class info. @return Whether this class info need update.
*/
- (BOOL)needUpdate; /**
Get the class info of a specified Class. @discussion This method will cache the class info and super-class info
at the first access to the Class. This method is thread-safe. @param cls A class.
@return A class info, or nil if an error occurs.
*/
+ (nullable instancetype)classInfoWithClass:(Class)cls; /**
Get the class info of a specified Class. @discussion This method will cache the class info and super-class info
at the first access to the Class. This method is thread-safe. @param className A class name.
@return A class info, or nil if an error occurs.
*/
+ (nullable instancetype)classInfoWithClassName:(NSString *)className;
这里需要注意的是两个方法
/**
If the class is changed (for example: you add a method to this class with
'class_addMethod()'), you should call this method to refresh the class info cache. After called this method, `needUpdate` will returns `YES`, and you should call
'classInfoWithClass' or 'classInfoWithClassName' to get the updated class info.
*/
- (void)setNeedUpdate;
当使用运行时对本类 操作的时候 ,需要调用方法更新本类的信息,这个时候
needUpdate
将返回为yes 让后调用
`classInfoWithClass` or `classInfoWithClassName`
具体的作用后边还会详细的解释,接下来看看方法的具体实现
+ (instancetype)classInfoWithClass:(Class)cls { // 判空
if (!cls) return nil;
/**
* 在此使用了coreFoundation 的 CFMutableDictionaryRef
* 单例模式
*/ static CFMutableDictionaryRef classCache;
static CFMutableDictionaryRef metaCache;
static dispatch_once_t onceToken;
/**
* GCD 信号
*/
static dispatch_semaphore_t lock;
dispatch_once(&onceToken, ^{
classCache = CFDictionaryCreateMutable(CFAllocatorGetDefault(), , &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
metaCache = CFDictionaryCreateMutable(CFAllocatorGetDefault(), , &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
lock = dispatch_semaphore_create();
}); // 加锁
dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER); /**
* 首先在缓存中取值,分为两种,metaCache , classCache
*/
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);
CFDictionarySetValue(info.isMeta ? metaCache : classCache, (__bridge const void *)(cls), (__bridge const void *)(info));
dispatch_semaphore_signal(lock);
}
}
return info;
}
上边的代码用到了几个我们平时可能不太熟悉的东西,
总体来说,但凡是需要反复使用的东西,尽量做缓存操作,这应该算是一种思想吧
dispatch_semaphore_t
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); 如果semaphore计数大于等于1.计数-1,返回,程序继续运行。
如果计数为0,则等待。
这里设置的等待时间是一直等待。dispatch_semaphore_signal(semaphore);计数+1
.在这两句代码中间的执行代码,每次只会允许一个线程进入,这样就有效的保证了在多线程环境下,只能有一个线程进入
这样就可以当做同步锁来使用了 。
CoreFoundation 框架的简单介绍
Core Foundation 是由oc base foundation 概念上衍生出来的一个库,只提供有限的基于C语言的oc的部分对象,
优点是 可以在frameworks and libraries 共享数据,使用coreFoundation 可以osX ios 运行
在平时使用中,我们可能更加关心的是
Toll-Free Bridged
也就是CF 和 NS 的转换问题
Objective-C 的ARC 内存管理 只管理 oc 的对象,对core foundation 是无效的,core foundation 必须通过 CFRelease来释放
对于MRC 可以直接进行转换,下面我们讨论 ARC 下的转换情况
id obj = [[NSObject alloc] init];
void *p = obj;
系统会直接报错,错误信息为
Implicit conversion of Objective-C pointer type 'id' to C pointer type 'void *' requires a bridged cast
很明显 ,需要一个bridged 来辅助转换
id obj = [[NSObject alloc] init];
void *p = (__bridge void *)obj;
ok,成功了 ,其实与这个bridged 相关的还有另外两个
__bridge_retained
__bridge_transfer
__bridge
只是单纯地执行了类型转换,没有进行所有权的转移,也就是说,当string对象被释放的时候,cfString也不能被使用了。
__bridge_retained
可以通过转换目标处(cfString)的 retain 处理,来使所有权转移。即使 string 变量被释放,cfString 还是可以使用具体的对象。只是有一点,由于Core Foundation的对象不属于ARC的管理范畴,所以需要自己release。
__bridge_transfer
所有权被转移的同时,被转换变量将失去对象的所有权。当Core Foundation对象类型向Objective-C对象类型转换的时候,会经常用到 __bridge_transfer 关键字。
CFStringRef cfString = CFStringCreate...();
NSString *string = (__bridge_transfer NSString *)cfString; // CFRelease(cfString); 因为已经用 __bridge_transfer 转移了对象的所有权,所以不需要调用 release
CFTypeRef CFBridgingRetain(id X) {
return (__bridge_retained CFTypeRef)X;
} id CFBridgingRelease(CFTypeRef X) {
return (__bridge_transfer id)X;
}
其他两个初始化方法
- (instancetype)initWithClass:(Class)cls {
if (!cls) return nil;
self = [super init];
_cls = cls;
_superCls = class_getSuperclass(cls);
_isMeta = class_isMetaClass(cls);
if (!_isMeta) {
_metaCls = objc_getMetaClass(class_getName(cls));
}
_name = NSStringFromClass(cls);
[self _update]; _superClassInfo = [self.class classInfoWithClass:_superCls];
return self;
}
+ (instancetype)classInfoWithClassName:(NSString *)className {
Class cls = NSClassFromString(className);
return [self classInfoWithClass:cls];
}
与needUpdate 想关
- (void)setNeedUpdate {
_needUpdate = YES;
} - (BOOL)needUpdate {
return _needUpdate;
}
- (void)_update {
_ivarInfos = nil;
_methodInfos = nil;
_propertyInfos = nil; Class cls = self.cls;
unsigned int methodCount = ;
Method *methods = class_copyMethodList(cls, &methodCount);
if (methods) {
NSMutableDictionary *methodInfos = [NSMutableDictionary new];
_methodInfos = methodInfos;
for (unsigned int i = ; i < methodCount; i++) {
YYClassMethodInfo *info = [[YYClassMethodInfo alloc] initWithMethod:methods[i]];
if (info.name) methodInfos[info.name] = info;
}
free(methods);
}
unsigned int propertyCount = ;
objc_property_t *properties = class_copyPropertyList(cls, &propertyCount);
if (properties) {
NSMutableDictionary *propertyInfos = [NSMutableDictionary new];
_propertyInfos = propertyInfos;
for (unsigned int i = ; i < propertyCount; i++) {
YYClassPropertyInfo *info = [[YYClassPropertyInfo alloc] initWithProperty:properties[i]];
if (info.name) propertyInfos[info.name] = info;
}
free(properties);
} unsigned int ivarCount = ;
Ivar *ivars = class_copyIvarList(cls, &ivarCount);
if (ivars) {
NSMutableDictionary *ivarInfos = [NSMutableDictionary new];
_ivarInfos = ivarInfos;
for (unsigned int i = ; i < ivarCount; i++) {
YYClassIvarInfo *info = [[YYClassIvarInfo alloc] initWithIvar:ivars[i]];
if (info.name) ivarInfos[info.name] = info;
}
free(ivars);
} if (!_ivarInfos) _ivarInfos = @{};
if (!_methodInfos) _methodInfos = @{};
if (!_propertyInfos) _propertyInfos = @{}; _needUpdate = NO;
}