IOS 中runtime使用方法整理
IOS 中runtime使用方法整理
做iOS的朋友都知道或聽說(shuō)runtime,這個(gè)東西很像java的反射機(jī)制,但功能遠(yuǎn)勝于java的反射。通過runtime我們可以動(dòng)態(tài)的向一個(gè)類中添加屬性、成員變量、方法,以及對(duì)其進(jìn)行讀寫訪問。
新建兩個(gè)類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.給類添加方法
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.添加屬性(方式一)
//屬性類型
objc_property_attribute_t type = { "T", "@\"NSString\"" };
//訪問類型
objc_property_attribute_t ownership = { "C", "" };
//對(duì)應(yī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(@"通過Associate設(shè)置:%@",value);
5.獲取類的名稱
ClassOne *one = [ClassOne new]; const char *className = object_getClassName(one); NSLog(@"className:%@",[NSString stringWithUTF8String:className]);
6.獲取一個(gè)類的所有方法
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è)類的所有屬性
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]);
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- 總結(jié)iOS中runtime的使用
- iOS runtime forwardInvocation詳解及整理
- iOS使用runtime修改文本框(TextField)的占位文字顏色
- iOS runtime動(dòng)態(tài)添加方法示例詳解
- iOS通過Runtime實(shí)現(xiàn)友盟統(tǒng)計(jì)的實(shí)例代碼
- IOS Object-C 中Runtime詳解及實(shí)例代碼
- iOS利用Runtime實(shí)現(xiàn)友盟頁(yè)面數(shù)據(jù)統(tǒng)計(jì)的功能示例
- iOS runtime知識(shí)梳理
- iOS中Runtime的幾種基本用法記錄
- iOS Runtime詳解(新手也看得懂)
相關(guān)文章
iOS實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
IOS應(yīng)用內(nèi)跳轉(zhuǎn)系統(tǒng)設(shè)置相關(guān)界面的方法
在iOS開發(fā)中,有時(shí)會(huì)有跳轉(zhuǎn)系統(tǒng)設(shè)置界面的需求,例如提示用戶打開藍(lán)牙或者WIFI,提醒用戶打開推送或者位置權(quán)限等,接下來(lái)通過本文給大家介紹IOS應(yīng)用內(nèi)跳轉(zhuǎn)系統(tǒng)設(shè)置相關(guān)界面的方法,喜歡的朋友參考下2016-02-02
iOS開發(fā)中class和#import的區(qū)別介紹
這篇文章主要介紹了iOS開發(fā)中class和#import的區(qū)別,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-02-02
iOS 把圖片保存到相冊(cè),并獲取圖片文件名的實(shí)例
下面小編就為大家分享一篇iOS 把圖片保存到相冊(cè),并獲取圖片文件名的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2017-12-12

