iOS runtime应用总结

一些公用类:

@interface CustomClass : NSObject

- (void) fun1;

@end

@implementation CustomClass

- (void) fun1

{

NSLog(@"fun1");

}

@end

@interface TestClass : NSObject

@end

@implementation TestClass

@end

别忘记引入库:#include <objc/runtime.h>

1、对象拷贝:id object_copy(id obj, size_t size)

- (void) copyObj

{

CustomClass *obj = [CustomClassnew];

NSLog(@"%p", &obj);

id objTest = object_copy(obj,sizeof(obj));

NSLog(@"%p", &objTest);

[objTest fun1];

}

打印结果:

2013-07-26 15:35:11.547 HighOC[6859:c07] 0xbfffdf64

2013-07-26 15:35:11.547 HighOC[6859:c07] 0xbfffdf60

2013-07-26 15:35:11.547 HighOC[6859:c07] fun1

说明:

object_copy 函数实现了对象的拷贝。

2、对象释放 id object_dispose(id obj)

- (void) objectDispose

{

CustomClass *obj = [CustomClassnew];

object_dispose(obj);

[obj release];

[obj fun1];

}

打印结果:程序crash

malloc: *** error for object 0x758e6d0: pointer being freed was not allocated

3、更改对象的类/获取对象的类

Class object_setClass(id obj, Class cls)  /    Class object_getClass(id obj)

- (void) setClassTest

{

CustomClass *obj = [CustomClassnew];

[obj fun1];

Class aClass =object_setClass(obj, [CustomClassOtherclass]);

//obj 对象的类被更改了    swap the isa to an isa

NSLog(@"aClass:%@",NSStringFromClass(aClass));

NSLog(@"obj class:%@",NSStringFromClass([objclass]));

[obj fun2];

}

- (void) getClassTest

{

CustomClass *obj = [CustomClassnew];

Class aLogClass =object_getClass(obj);

NSLog(@"%@",NSStringFromClass(aLogClass));

}

4、获取对象的类名  constchar *object_getClassName(id obj)

- (void) getClassName

{

CustomClass *obj = [CustomClassnew];

NSString *className = [NSStringstringWithCString:object_getClassName(obj)encoding:NSUTF8StringEncoding];

NSLog(@"className:%@", className);

}

5、给一个类添加方法

BOOL class_addMethod(Class cls,SEL name,IMP imp,    const char *types)

/**

* 一个参数

*

*/

int cfunction(id self, SEL _cmd, NSString *str) {

NSLog(@"%@", str);

return10;//随便返回个值

}

- (void) oneParam {

TestClass *instance = [[TestClassalloc]init];

//    方法添加

class_addMethod([TestClassclass],@selector(ocMethod:), (IMP)cfunction,"i@:@");

if ([instance respondsToSelector:@selector(ocMethod:)]) {

NSLog(@"Yes, instance respondsToSelector:@selector(ocMethod:)");

} else

{

NSLog(@"Sorry");

}

int a = (int)[instanceocMethod:@"我是一个OC的method,C函数实现"];

NSLog(@"a:%d", a);

}

/**

* 两个参数

*

*/

int cfunctionA(id self, SEL _cmd, NSString *str, NSString *str1) {

NSLog(@"%@-%@", str, str1);

return20;//随便返回个值

}

- (void) twoParam {

TestClass *instance = [[TestClassalloc]init];

class_addMethod([TestClassclass],@selector(ocMethodA::), (IMP)cfunctionA,"i@:@@");

if ([instance respondsToSelector:@selector(ocMethodA::)]) {

NSLog(@"Yes, instance respondsToSelector:@selector(ocMethodA::)");

} else

{

NSLog(@"Sorry");

}

int a = (int)[instanceocMethodA:@"我是一个OC的method,C函数实现" :@"-----我是第二个参数"];

NSLog(@"a:%d", a);

}

相关文档及说明:

Obj-C的方法(method)就是一个至少需要两个参数(self,_cmd)的C函数

IMP有点类似函数指针,指向具体的Method实现。

向一个类动态添加方法

BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)

参数说明:

cls:被添加方法的类

name:可以理解为方法名

imp:实现这个方法的函数

types:一个定义该函数返回值类型和参数类型的字符串

class_addMethod([TestClass class], @selector(ocMethod:), (IMP)testFunc, "i@:@");

其中types参数为"i@:@“,按顺序分别表示:

i:返回值类型int,若是v则表示void

@:参数id(self)

::SEL(_cmd)

@:id(str)

一些公用类:

@interface ClassCustomClass :NSObject{

NSString *varTest1;

NSString *varTest2;

NSString *varTest3;

}

@property (nonatomic,assign)NSString *varTest1;

@property (nonatomic,assign)NSString *varTest2;

@property (nonatomic,assign)NSString *varTest3;

- (void) fun1;

@end

@implementation ClassCustomClass

@synthesize varTest1, varTest2, varTest3;

- (void) fun1 {

NSLog(@"fun1");

}

@end

@interface ClassCustomClassOther :NSObject {

int varTest2;

}

- (void) fun2;

@end

@implementation ClassCustomClassOther

- (void) fun2 {

NSLog(@"fun2");

}

@end

@interface ClassPropertyViewCtr () {

float myFloat;

ClassCustomClass *allobj;

}

myFloat = 2.34f;

6、获取一个类的所有方法:

- (void) getClassAllMethod

{

u_int count;

Method* methods= class_copyMethodList([UIViewController class], &count);

for (int i = 0; i < count ; i++)

{

SEL name = method_getName(methods[i]);

NSString *strName = [NSString stringWithCString:sel_getName(name)encoding:NSUTF8StringEncoding];

NSLog(@"%@",strName);

}

}

打印结果(部分):

2013-07-26 16:07:03.972 HighOC[7021:c07] _screen

2013-07-26 16:07:03.973 HighOC[7021:c07] applicationWillSuspend

2013-07-26 16:07:03.973 HighOC[7021:c07] _tryBecomeRootViewControllerInWindow:

2013-07-26 16:07:03.973 HighOC[7021:c07] isViewLoaded

2013-07-26 16:07:03.974 HighOC[7021:c07] view

......................

7、获取一个类的所有属性:

- (void) propertyNameList

{

u_int count;

objc_property_t *properties=class_copyPropertyList([UIViewControllerclass], &count);

for (int i = 0; i < count ; i++)

{

const char* propertyName =property_getName(properties[i]);

NSString *strName = [NSString stringWithCString:propertyNameencoding:NSUTF8StringEncoding];

NSLog(@"%@",strName);

}

}

打印结果(部分)

2013-07-26 16:09:42.182 HighOC[7041:c07] tabBarItem

2013-07-26 16:09:42.184 HighOC[7041:c07] tabBarController

2013-07-26 16:09:42.185 HighOC[7041:c07] splitViewController

2013-07-26 16:09:42.186 HighOC[7041:c07] navigationItem

2013-07-26 16:09:42.186 HighOC[7041:c07] hidesBottomBarWhenPushed

...............

8、获取/设置类的属性变量

//获取全局变量的值  (myFloat 为类的一个属性变量)

- (void) getInstanceVar {

float myFloatValue;

object_getInstanceVariable(self,"myFloat", (void*)&myFloatValue);

NSLog(@"%f", myFloatValue);

}

//设置全局变量的值

- (void) setInstanceVar {

float newValue = 10.00f;

unsigned int addr = (unsignedint)&newValue;

object_setInstanceVariable(self,"myFloat", *(float**)addr);

NSLog(@"%f", myFloat);

}

9、判断类的某个属性的类型

- (void) getVarType {

ClassCustomClass *obj = [ClassCustomClassnew];

Ivar var = class_getInstanceVariable(object_getClass(obj),"varTest1");

const char* typeEncoding =ivar_getTypeEncoding(var);

NSString *stringType =  [NSStringstringWithCString:typeEncodingencoding:NSUTF8StringEncoding];

if ([stringType hasPrefix:@"@"]) {

// handle class case

NSLog(@"handle class case");

} else if ([stringTypehasPrefix:@"i"]) {

// handle int case

NSLog(@"handle int case");

} else if ([stringTypehasPrefix:@"f"]) {

// handle float case

NSLog(@"handle float case");

} else

{

}

}

10、通过属性的值来获取其属性的名字(反射机制)

- (NSString *)nameOfInstance:(id)instance

{

unsigned int numIvars =0;

NSString *key=nil;

//Describes the instance variables declared by a class.

Ivar * ivars = class_copyIvarList([ClassCustomClassclass], &numIvars);

for(int i = 0; i < numIvars; i++) {

Ivar thisIvar = ivars[i];

const char *type =ivar_getTypeEncoding(thisIvar);

NSString *stringType =  [NSStringstringWithCString:typeencoding:NSUTF8StringEncoding];

//不是class就跳过

if (![stringType hasPrefix:@"@"]) {

continue;

}

//Reads the value of an instance variable in an object. object_getIvar这个方法中,当遇到非objective-c对象时,并直接crash

if ((object_getIvar(allobj, thisIvar) == instance)) {

// Returns the name of an instance variable.

key = [NSStringstringWithUTF8String:ivar_getName(thisIvar)];

break;

}

}

free(ivars);

return key;

}

测试代码:

allobj = [ClassCustomClassnew];

allobj.varTest1 =@"varTest1String";

allobj.varTest2 =@"varTest2String";

allobj.varTest3 =@"varTest3String";

NSString *str = [selfnameOfInstance:@"varTest1String"];

NSLog(@"str:%@", str);

打印结果:

str:varTest1

11、系统类的方法实现部分替换

- (void) methodExchange {

Method m1 = class_getInstanceMethod([NSStringclass],@selector(lowercaseString));

Method m2 = class_getInstanceMethod([NSStringclass],@selector(uppercaseString));

method_exchangeImplementations(m1, m2);

NSLog(@"%@", [@"sssAAAAss"lowercaseString]);

NSLog(@"%@", [@"sssAAAAss"uppercaseString]);

}

打印结果:(仔细看log)

2013-07-26 16:33:22.776 HighOC[7104:c07] SSSAAAASS

2013-07-26 16:33:22.778 HighOC[7104:c07] sssaaaass

12、自定义类的方法实现部分替换

- (void) justLog1 {

NSLog(@"justLog1");

}

- (void) justLog2 {

NSLog(@"justLog2");

}

- (void) methodSetImplementation {

Method method = class_getInstanceMethod([ClassMethodViewCtrclass],@selector(justLog1));

IMP originalImp = method_getImplementation(method);

Method m1 = class_getInstanceMethod([ClassMethodViewCtrclass],@selector(justLog2));

method_setImplementation(m1, originalImp);

}

//[self methodSetImplementation];

//[self justLog2];

13、覆盖系统方法

IMP cFuncPointer;

IMP cFuncPointer1;

IMP cFuncPointer2;

NSString* CustomUppercaseString(idself,SEL_cmd){

printf("真正起作用的是本函数CustomUppercaseString\r\n");

NSString *string = cFuncPointer(self,_cmd);

return string;

}

NSArray* CustomComponentsSeparatedByString(idself,SEL_cmd,NSString *str){

printf("真正起作用的是本函数CustomIsEqualToString\r\n");

return cFuncPointer1(self,_cmd, str);

}

//不起作用,求解释

bool CustomIsEqualToString(idself,SEL_cmd,NSString *str) {

printf("真正起作用的是本函数CustomIsEqualToString\r\n");

return cFuncPointer2(self,_cmd, str);

}

- (void) replaceMethod{

cFuncPointer = [NSStringinstanceMethodForSelector:@selector(uppercaseString)];

class_replaceMethod([NSStringclass],@selector(uppercaseString), (IMP)CustomUppercaseString,"@@:");

cFuncPointer1 = [NSStringinstanceMethodForSelector:@selector(componentsSeparatedByString:)];

class_replaceMethod([NSStringclass],@selector(componentsSeparatedByString:), (IMP)CustomComponentsSeparatedByString,"@@:@");

cFuncPointer2 = [NSStringinstanceMethodForSelector:@selector(isEqualToString:)];

class_replaceMethod([NSStringclass],@selector(isEqualToString:), (IMP)CustomIsEqualToString,"B@:@");

}

14、自动序列化(转)

#import "NSObject+AutoEncodeDecode.h"

@implementation NSObject (AutoEncodeDecode)

- (void)encodeWithCoder:(NSCoder *)encoder {

Class cls = [selfclass];

while (cls != [NSObjectclass]) {

unsigned int numberOfIvars =0;

Ivar* ivars = class_copyIvarList(cls, &numberOfIvars);

for(const Ivar* p = ivars; p < ivars+numberOfIvars; p++){

Ivar const ivar = *p;

const char *type =ivar_getTypeEncoding(ivar);

NSString *key = [NSStringstringWithUTF8String:ivar_getName(ivar)];

id value = [selfvalueForKey:key];

if (value) {

switch (type[0]) {

case _C_STRUCT_B: {

NSUInteger ivarSize =0;

NSUInteger ivarAlignment =0;

NSGetSizeAndAlignment(type, &ivarSize, &ivarAlignment);

NSData *data = [NSDatadataWithBytes:(constchar *)self + ivar_getOffset(ivar)

length:ivarSize];

[encoder encodeObject:dataforKey:key];

}

break;

default:

[encoder encodeObject:value

forKey:key];

break;

}

}

}

free(ivars);

cls = class_getSuperclass(cls);

}

}

- (id)initWithCoder:(NSCoder *)decoder {

self = [self init];

if (self) {

Class cls = [selfclass];

while (cls != [NSObjectclass]) {

unsigned int numberOfIvars =0;

Ivar* ivars = class_copyIvarList(cls, &numberOfIvars);

for(constIvar* p = ivars; p < ivars+numberOfIvars; p++){

Ivar const ivar = *p;

const char *type =ivar_getTypeEncoding(ivar);

NSString *key = [NSStringstringWithUTF8String:ivar_getName(ivar)];

id value = [decoder decodeObjectForKey:key];

if (value) {

switch (type[0]) {

case _C_STRUCT_B: {

NSUInteger ivarSize =0;

NSUInteger ivarAlignment =0;

NSGetSizeAndAlignment(type, &ivarSize, &ivarAlignment);

NSData *data = [decoderdecodeObjectForKey:key];

char *sourceIvarLocation = (char*)self+ivar_getOffset(ivar);

[data getBytes:sourceIvarLocationlength:ivarSize];

}

break;

default:

[self setValue:[decoder decodeObjectForKey:key]

forKey:key];

break;

}

}

}

free(ivars);

cls = class_getSuperclass(cls);

}

}

return self;

}

用C代替OC:#import#import#importextern int UIApplicationMain (int argc,char *argv[],void *principalClassName,void *delegateClassName);

struct Rect {

float x;

float y;

float width;

float height;

};

typedef struct Rect Rect;

void *navController;

static int numberOfRows =100;

int tableView_numberOfRowsInSection(void *receiver,structobjc_selector *selector, void *tblview,int section) {

returnnumberOfRows;

}

void *tableView_cellForRowAtIndexPath(void *receiver,structobjc_selector *selector, void *tblview,void *indexPath) {

Class TableViewCell = (Class)objc_getClass("UITableViewCell");

void *cell = class_createInstance(TableViewCell,0);

objc_msgSend(cell, sel_registerName("init"));

char buffer[7];

int row = (int) objc_msgSend(indexPath, sel_registerName("row"));

sprintf (buffer, "Row %d", row);

void *label =objc_msgSend(objc_getClass("NSString"),sel_registerName("stringWithUTF8String:"),buffer);

objc_msgSend(cell, sel_registerName("setText:"),label);

return cell;

}

void tableView_didSelectRowAtIndexPath(void *receiver,structobjc_selector *selector, void *tblview,void *indexPath) {

Class ViewController = (Class)objc_getClass("UIViewController");

void * vc = class_createInstance(ViewController,0);

objc_msgSend(vc, sel_registerName("init"));

char buffer[8];

int row = (int) objc_msgSend(indexPath, sel_registerName("row"));

sprintf (buffer, "Item %d", row);

void *label =objc_msgSend(objc_getClass("NSString"),sel_registerName("stringWithUTF8String:"),buffer);

objc_msgSend(vc, sel_registerName("setTitle:"),label);

objc_msgSend(navController,sel_registerName("pushViewController:animated:"),vc,1);

}

void *createDataSource() {

Class superclass = (Class)objc_getClass("NSObject");

Class DataSource = objc_allocateClassPair(superclass,"DataSource",0);

class_addMethod(DataSource,sel_registerName("tableView:numberOfRowsInSection:"), (void(*))tableView_numberOfRowsInSection,nil);

class_addMethod(DataSource,sel_registerName("tableView:cellForRowAtIndexPath:"), (void(*))tableView_cellForRowAtIndexPath,nil);

objc_registerClassPair(DataSource);

returnclass_createInstance(DataSource,0);

}

void * createDelegate() {

Class superclass = (Class)objc_getClass("NSObject");

Class DataSource = objc_allocateClassPair(superclass,"Delegate",0);

class_addMethod(DataSource,sel_registerName("tableView:didSelectRowAtIndexPath:"), (void(*))tableView_didSelectRowAtIndexPath,nil);

objc_registerClassPair(DataSource);

returnclass_createInstance(DataSource,0);

}

void applicationdidFinishLaunching(void *receiver,structobjc_selector *selector, void *application) {

Class windowClass = (Class)objc_getClass("UIWindow");

void * windowInstance = class_createInstance(windowClass, 0);

objc_msgSend(windowInstance, sel_registerName("initWithFrame:"),(Rect){0,0,320,480});

//Make Key and Visiable

objc_msgSend(windowInstance,sel_registerName("makeKeyAndVisible"));

//Create Table View

Class TableViewController = (Class)objc_getClass("UITableViewController");

void *tableViewController = class_createInstance(TableViewController, 0);

objc_msgSend(tableViewController, sel_registerName("init"));

void *tableView = objc_msgSend(tableViewController,sel_registerName("tableView"));

objc_msgSend(tableView, sel_registerName("setDataSource:"),createDataSource());

objc_msgSend(tableView, sel_registerName("setDelegate:"),createDelegate());

Class NavController = (Class)objc_getClass("UINavigationController");

navController = class_createInstance(NavController,0);

objc_msgSend(navController,sel_registerName("initWithRootViewController:"),tableViewController);

void *view =objc_msgSend(navController,sel_registerName("view"));

//Add Table View To Window

objc_msgSend(windowInstance, sel_registerName("addSubview:"),view);

}

//Create an class named "AppDelegate", and return it's name as an instance of class NSString

void *createAppDelegate() {

Class mySubclass = objc_allocateClassPair((Class)objc_getClass("NSObject"),"AppDelegate",0);

structobjc_selector *selName =sel_registerName("application:didFinishLaunchingWithOptions:");

class_addMethod(mySubclass, selName, (void(*))applicationdidFinishLaunching,nil);

objc_registerClassPair(mySubclass);

returnobjc_msgSend(objc_getClass("NSString"),sel_registerName("stringWithUTF8String:"),"AppDelegate");

}

int main(int argc, char *argv[]) {

returnUIApplicationMain(argc, argv,0,createAppDelegate());

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,080评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,422评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,630评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,554评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,662评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,856评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,014评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,752评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,212评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,541评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,687评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,347评论 4 331
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,973评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,777评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,006评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,406评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,576评论 2 349

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,690评论 0 9
  • 我们常常会听说 Objective-C 是一门动态语言,那么这个「动态」表现在哪呢?我想最主要的表现就是 Obje...
    Ethan_Struggle阅读 2,182评论 0 7
  • 小时候性格内向,交到的好朋友很少,每天上学放学都是独自一人回家。 有一次放学被几个坏孩子围攻,零食和玩具被抢,我坐...
    Cc艺知猪阅读 670评论 1 5
  • 网页设计发展很快,它主要是跟着设计潮流以及Web技术一起演变。从纯文字的网页到FLASH、然后现在流行的HTML5...
    打豆豆阅读 3,327评论 2 70
  • 下雨了,才知道谁会给你送伞;遇事了,才知道谁对你真心。有些人,只会锦上添花,不会雪中送炭;有些人,只会火上浇油,不...
    罗掌柜real阅读 132评论 0 0