日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

iOS 进阶 - RUNTIME 运行时

發(fā)布時(shí)間:2025/3/20 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS 进阶 - RUNTIME 运行时 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

什么是 RUNTIME ?

1.Runtime就是運(yùn)行時(shí),OC就是運(yùn)行機(jī)制也就是在運(yùn)行的時(shí)候的一些機(jī)制。其主要有消息機(jī)制。

2.對(duì)于C語(yǔ)言來(lái)說(shuō),函數(shù)調(diào)用在編譯的時(shí)候就會(huì)決定調(diào)用哪個(gè)函數(shù);而對(duì)于OC的函數(shù),是屬于動(dòng)態(tài)調(diào)用過(guò)程的,在編譯的時(shí)候并不決定真正調(diào)用哪個(gè)函數(shù),只有在真正運(yùn)行的時(shí)候才會(huì)根據(jù)函數(shù)名稱去找到對(duì)應(yīng)的函數(shù)來(lái)調(diào)用。

3.在編譯階段,OC可以調(diào)用任何函數(shù),即使這個(gè)函數(shù)并未實(shí)現(xiàn),只有存在聲明就不會(huì)報(bào)錯(cuò)。而C語(yǔ)言在編譯階段調(diào)用未實(shí)現(xiàn)的方法是會(huì)報(bào)錯(cuò)的。

?

如何 RUNTIME 通過(guò)獲取屬性和方法名?

【oschina】http://git.oschina.net/emo_lin/RUNTIME

1. 獲取該類中.h和.m中成員變量,可以通過(guò) class_copyPropertyList 來(lái)實(shí)現(xiàn)。

// 獲取該類中.h和.m中成員變量,可以通過(guò) class_copyPropertyList 來(lái)實(shí)現(xiàn)。 -(NSArray *)allPropertyies {unsigned int count;objc_property_t * property = class_copyPropertyList([self class], &count);NSMutableArray * propertyieArray = [NSMutableArray arrayWithCapacity:count];for (NSUInteger i = 0; i < count; i++) {const char * propertyName = property_getName(property[i]);NSString * name = [NSString stringWithUTF8String:propertyName];[propertyieArray addObject:name];}free(property);return propertyieArray; }

2.獲取對(duì)象的有值的屬性名和屬性值,如果屬性名沒(méi)有值需要為其賦空。

// 獲取對(duì)象的有值的屬性名和屬性值,如果屬性名沒(méi)有值需要為其賦空。 - (NSDictionary *)allPropertyNamesAndValues {NSMutableDictionary *resultDict = [NSMutableDictionary dictionary];unsigned int outCount;// 開(kāi)辟內(nèi)存空間objc_property_t *properties = class_copyPropertyList([self class], &outCount);for (int i = 0; i < outCount; i++) {objc_property_t property = properties[i];const char *name = property_getName(property);// 得到屬性名NSString *propertyName = [NSString stringWithUTF8String:name];// 獲取屬性值id propertyValue = [self valueForKey:propertyName];if (propertyValue && propertyValue != nil) {[resultDict setObject:propertyValue forKey:propertyName];}}// 釋放內(nèi)存空間free(properties);return resultDict; }

3. 獲取該類中的所有方法時(shí),包括通過(guò)@property為成員變量自動(dòng)生成的setter和getter方法,可以通過(guò)class_copyMethodList來(lái)實(shí)現(xiàn)。

// 獲取該類中的所有方法時(shí),包括通過(guò)@property為成員變量自動(dòng)生成的setter和getter方法,可以通過(guò)class_copyMethodList來(lái)實(shí)現(xiàn)。 - (void)allMethods {unsigned int outCount = 0;Method *methods = class_copyMethodList([self class], &outCount);for (int i = 0; i < outCount; ++i) {Method method = methods[i];// 獲取方法名稱,但是類型是一個(gè)SEL選擇器類型SEL methodSEL = method_getName(method);// 需要獲取C字符串const char *name = sel_getName(methodSEL);// 將方法名轉(zhuǎn)換成OC字符串NSString *methodName = [NSString stringWithUTF8String:name];// 獲取方法的參數(shù)列表int arguments = method_getNumberOfArguments(method);NSLog(@"方法名:%@, 參數(shù)個(gè)數(shù):%d", methodName, arguments);}// 記得釋放free(methods); }

4.獲取所有的私有成員變量,可以通過(guò)class_copyIvarList實(shí)現(xiàn)。

// 獲取所有的私有成員變量,可以通過(guò)class_copyIvarList實(shí)現(xiàn)。 - (NSArray *)allMemberVariables {unsigned int count = 0;Ivar *ivars = class_copyIvarList([self class], &count);NSMutableArray *results = [[NSMutableArray alloc] init];for (NSUInteger i = 0; i < count; ++i) {Ivar variable = ivars[i];const char *name = ivar_getName(variable);NSString *varName = [NSString stringWithUTF8String:name];[results addObject:varName];}free(ivars);return results; }

?

?

?

?

轉(zhuǎn)載于:https://my.oschina.net/linweida/blog/742859

總結(jié)

以上是生活随笔為你收集整理的iOS 进阶 - RUNTIME 运行时的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。