Runtime是想要作好iOS開發,或者說是真正的深入的掌握OC這門語言所必需理解的東西。最近在學習Runtime,有本身的一些心得,整理以下,學習
一爲 查閱方便fetch
二爲 或許能給他人一些啓發,設計
三爲 但願獲得你們對這篇整理不足之處的一些指點。對象
什麼是Runtimeblog
咱們寫的代碼在程序運行過程當中都會被轉化成runtime的C代碼執行,例如[target doSomething];會被轉化成objc_msgSend(target, @selector(doSomething));。ip
OC中一切都被設計成了對象,咱們都知道一個類被初始化成一個實例,這個實例是一個對象。實際上一個類本質上也是一個對象,在runtime中用結構體表示。ci
可是單純的去調用又顯得很麻煩,全部就本身簡單的封裝了幾個方法,能夠和你們一塊兒交流學習開發
/** 獲取類名 @param class <#class description#> @return <#return value description#> */ + (NSString *)fetchClassName:(Class)class{ const char *className = class_getName(class); return [NSString stringWithUTF8String:className]; } /** 獲取成員變量 @param class <#class description#> @return <#return value description#> */ + (NSArray*)fetchIvaList:(Class)class{ unsigned int count = 0; Ivar *ivarList = class_copyIvarList(class, &count); NSMutableArray *mutaList = [[NSMutableArray alloc]initWithCapacity:count]; for (unsigned int i=0; i<count; i++) { NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:2]; const char*ivarName = ivar_getName(ivarList[i]); const char*ivarType = ivar_getTypeEncoding(ivarList[i]); dic[@"type"] = [NSString stringWithUTF8String:ivarType]; dic[@"ivarName"] = [NSString stringWithUTF8String:ivarName]; [mutaList addObject:dic]; } free(ivarList); return [NSArray arrayWithArray:mutaList]; /** 獲取類的屬性 @param NSArray <#NSArray description#> @return <#return value description#> */ } + (NSArray*)fetchPropertyList:(Class)class{ unsigned int count =0; objc_property_t*propertyList = class_copyPropertyList(class, &count); NSMutableArray *mutableList = [[NSMutableArray alloc]initWithCapacity:count]; for (unsigned int i=0; i<count; i++) { const char*propertyName = property_getName(propertyList[i]); [mutableList addObject:[NSString stringWithUTF8String:propertyName]]; } free(propertyList); return [NSArray arrayWithArray:mutableList]; } /** 類的實例方法 @param class <#class description#> @return <#return value description#> */ + (NSArray*)fetchMethodList:(Class)class{ unsigned int count = 0; Method *methodList = class_copyMethodList(class, &count); NSMutableArray *mutableList = [NSMutableArray arrayWithCapacity:count]; for (unsigned int i=0; i<count; i++) { Method method = methodList[i]; SEL methodName = method_getName(method); [mutableList addObject:NSStringFromSelector(methodName)]; } free(methodList); return [NSArray arrayWithArray:mutableList]; }