runtime如何在IOS中使用

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)runtime如何在IOS 中使用,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)公司主要為客戶(hù)提供服務(wù)項(xiàng)目涵蓋了網(wǎng)頁(yè)視覺(jué)設(shè)計(jì)、VI標(biāo)志設(shè)計(jì)、成都營(yíng)銷(xiāo)網(wǎng)站建設(shè)、網(wǎng)站程序開(kāi)發(fā)、HTML5響應(yīng)式重慶網(wǎng)站建設(shè)公司手機(jī)網(wǎng)站制作、微商城、網(wǎng)站托管及成都網(wǎng)站維護(hù)、WEB系統(tǒng)開(kāi)發(fā)、域名注冊(cè)、國(guó)內(nèi)外服務(wù)器租用、視頻、平面設(shè)計(jì)、SEO優(yōu)化排名。設(shè)計(jì)、前端、后端三個(gè)建站步驟的完善服務(wù)體系。一人跟蹤測(cè)試的建站服務(wù)標(biāo)準(zhǔn)。已經(jīng)為混凝土攪拌罐行業(yè)客戶(hù)提供了網(wǎng)站營(yíng)銷(xiāo)服務(wù)。

新建兩個(gè)類(lèi)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.拷貝對(duì)象

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

2.給類(lèi)添加方法

ClassOne *one = [ClassOne new];
class_addMethod([ClassOne class], @selector(testClassOneWithArg1:arg2:arg3:), (IMP)testClassOne , "i@:@@@");
[one testClassOneWithArg1:@"arg1" arg2:@"arg2" arg3:@"arg3"];

//方法對(duì)應(yīng)的C函數(shù)
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.添加屬性(方式一)

//屬性類(lèi)型
objc_property_attribute_t type = { "T", "@\"NSString\"" };
//訪問(wèn)類(lèi)型
objc_property_attribute_t ownership = { "C", "" };
//對(duì)應(yīng)成員變量名稱(chēng)
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:@@@");


//屬性對(duì)應(yīng)的Getter方法
NSString* testPropertyNameGetter(id self,SEL _cmd){
  Ivar ivar = class_getInstanceVariable([ClassOne class], "_testPropertyName");
  return object_getIvar(self, ivar);
}

//屬性對(duì)應(yīng)的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(@"通過(guò)Associate設(shè)置:%@",value);

5.獲取類(lèi)的名稱(chēng)

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

6.獲取一個(gè)類(lèi)的所有方法

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.獲取一個(gè)類(lèi)的所有屬性

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.獲取類(lèi)的所有成員變量

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.獲得成員變量類(lèi)型

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 中使用了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁(yè)題目:runtime如何在IOS中使用
標(biāo)題路徑:http://muchs.cn/article32/iehipc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、定制開(kāi)發(fā)、網(wǎng)站制作、面包屑導(dǎo)航、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化