十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这期内容当中小编将会给大家带来有关runtime如何在IOS 中使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
创新互联公司主要为客户提供服务项目涵盖了网页视觉设计、VI标志设计、成都营销网站建设、网站程序开发、HTML5响应式重庆网站建设公司、手机网站制作、微商城、网站托管及成都网站维护、WEB系统开发、域名注册、国内外服务器租用、视频、平面设计、SEO优化排名。设计、前端、后端三个建站步骤的完善服务体系。一人跟踪测试的建站服务标准。已经为混凝土搅拌罐行业客户提供了网站营销服务。
新建两个类ClassOne和ClassTwo
#import@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@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]); }
上述就是小编为大家分享的runtime如何在IOS 中使用了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。