国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

IOS 中runtime使用方法整理

2019-10-21 18:47:18
字體:
來源:轉載
供稿:網友

IOS 中runtime使用方法整理

做iOS的朋友都知道或聽說runtime,這個東西很像java的反射機制,但功能遠勝于java的反射。通過runtime我們可以動態的向一個類中添加屬性、成員變量、方法,以及對其進行讀寫訪問。

新建兩個類ClassOne和ClassTwo

#import <Foundation/Foundation.h>@interface ClassOne : NSObject{  NSString *_publicVar1;  NSString *_publicVar2;}@property(nonatomic,copy) NSString *publicProperty1;@property(nonatomic,copy) NSString *publicProperty2;- (void) testClassOneWithArg1:(NSString *)arg1;@end#import "ClassOne.h"@interface ClassOne()@property(nonatomic,copy) NSString *privateProperty1;@property(nonatomic,copy) NSString *privateProperty2;@end@implementation ClassOne{    NSString *_privateVar1;    NSString *_privateVar2;}- (void)testClassOneWithArg1:(NSString *)arg1{  NSLog(@"this is CalssOne, arg1:%@",arg1);}- (void)testClassOneWithArg1:(NSString *)arg1 arg2:arg2{  NSLog(@"this is CalssOne, arg1:%@ arg2:%@",arg1,arg2);}@end
#import <Foundation/Foundation.h>@interface ClassTwo : NSObject- (void) testClassTwoWithArg1:(NSString *)arg1 arg2:(NSString *)arg2;@end#import "ClassTwo.h"@implementation ClassTwo- (void)testClassTwoWithArg1:(NSString *)arg1 arg2:(NSString *)arg2{  NSLog(@"this is ClassTwo arg1:%@,arg2:%@",arg1,arg2);}@end

1.拷貝對象

ClassOne *one = [ClassOne new];id onec1 = object_copy(one,sizeof(one));

2.給類添加方法

ClassOne *one = [ClassOne new];class_addMethod([ClassOne class], @selector(testClassOneWithArg1:arg2:arg3:), (IMP)testClassOne , "i@:@@@");[one testClassOneWithArg1:@"arg1" arg2:@"arg2" arg3:@"arg3"];//方法對應的C函數int testClassOne(id self,SEL _cmd, NSString *arg1,NSString *arg2,NSString *arg3){NSLog(@"this is a test function add to ClassOne as a methad with arg1:%@ arg2:%@ and arg3:%@",arg1,arg2,arg3);  return 10;}

3.添加屬性(方式一)

//屬性類型objc_property_attribute_t type = { "T", "@/"NSString/"" };//訪問類型objc_property_attribute_t ownership = { "C", "" };//對應成員變量名稱objc_property_attribute_t backingivar = { "V", "_testPropertyName" };objc_property_attribute_t attrs[] = { type, ownership, backingivar };class_addProperty([ClassOne class], "testPropertyName", attrs, 3);class_addMethod([ClassOne class], @selector(testPropertyName), (IMP)testPropertyNameGetter , "@:@@");class_addMethod([ClassOne class], @selector(setTestPropertyName:), (IMP)testPropertyNameSetter, "v:@@@");//屬性對應的Getter方法NSString* testPropertyNameGetter(id self,SEL _cmd){  Ivar ivar = class_getInstanceVariable([ClassOne class], "_testPropertyName");  return object_getIvar(self, ivar);}//屬性對應的Setter方法void testPropertyNameSetter(id self,SEL _cmd,NSString *testPropertyNameValue){  Ivar ivar = class_getInstanceVariable([ClassOne class], "_testPropertyName");  object_setIvar(self, ivar, testPropertyNameValue);}

4.添加屬性(方式2)

ClassOne *one = [ClassOne new];objc_setAssociatedObject(one, "objTag", @"value", OBJC_ASSOCIATION_COPY);NSString *value = objc_getAssociatedObject(one, "objTag");NSLog(@"通過Associate設置:%@",value);

5.獲取類的名稱

ClassOne *one = [ClassOne new];const char *className = object_getClassName(one);NSLog(@"className:%@",[NSString stringWithUTF8String:className]);

6.獲取一個類的所有方法

UInt count;Method *methods = class_copyMethodList([ClassOne class], &count);for (int i = 0; i < count; i++) {  Method method = methods[i];  SEL sel = method_getName(method);  NSLog(@"方法名:%@",NSStringFromSelector(sel));}

7.獲取一個類的所有屬性

uint propertyCount;objc_property_t *ps = class_copyPropertyList([ClassOne class], &propertyCount);for (uint i = 0; i < propertyCount; i++) {  objc_property_t property = ps[i];  const char *propertyName = property_getName(property);  const char *propertyAttributes = property_getAttributes(property);  NSLog(@"propertyName:%@",[NSString stringWithUTF8String:propertyName]);  NSLog(@"propertyAttributes:%@",[NSString stringWithUTF8String:propertyAttributes]);}

8.獲取類的所有成員變量

uint ivarCount;Ivar *ivars = class_copyIvarList([ClassOne class], &ivarCount);for (uint i = 0; i < ivarCount; i++) {  Ivar ivar = ivars[i];  const char *ivarName = ivar_getName(ivar);  NSLog(@"ivarName:%@",[NSString stringWithUTF8String:ivarName]);}

9.獲得成員變量類型

 

uint ivarCount;Ivar *ivars = class_copyIvarList([ClassOne class], &ivarCount);for (uint i = 0; i < ivarCount; i++) {  Ivar ivar = ivars[i];  const char *ivarName = ivar_getName(ivar);  const char *type = ivar_getTypeEncoding(ivar);  NSLog(@"ivarName=%@,type=%@",[NSString stringWithUTF8String:ivarName],[NSString stringWithUTF8String:type]);}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 怀集县| 眉山市| 张家川| 吉首市| 仁寿县| 墨竹工卡县| 周口市| 长子县| 湖南省| 泸州市| 施甸县| 庆阳市| 长武县| 祁阳县| 雅江县| 乐安县| 新昌县| 涞水县| 铜川市| 天祝| 突泉县| 越西县| 谷城县| 台东县| 武平县| 汪清县| 凯里市| 万州区| 贡觉县| 临沧市| 平凉市| 忻城县| 息烽县| 子洲县| 景德镇市| 海淀区| 汤原县| 石家庄市| 焦作市| 沅陵县| 绥阳县|