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,"[email protected]:@");

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,"[email protected]:@@");

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, "[email protected]:@");

其中types参数为"[email protected]:@“,按顺序分别表示:

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

@:参数id(self)

::SEL(_cmd)

@:id(str)

官方文档:

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html

0
时间: 2024-08-29 07:35:27

IOS高级开发~Runtime(一)的相关文章

IOS高级开发 runtime(1)

一. 简介 IOS 开发中灵活使用runtime 会提高我们的程序性能和开发速度.要想使用runtime,首先要引入系统的头文件. #import <objc/runtime.h> 当我们查看runtime.h的时候,我们会发现,其实runtime是很有条理的 const char *object_getClassName(id obj) //获取对象的类 Ivar *class_copyIvarList(Class cls, unsigned int *outCount) //获取类的变量列

(转发)IOS高级开发~Runtime(二)

一些公用类: @interface ClassCustomClass :NSObject{ NSString *varTest1; NSString *varTest2; NSString *varTest3; } @property (nonatomic,assign)NSString *varTest1; @property (nonatomic,assign)NSString *varTest2; @property (nonatomic,assign)NSString *varTest3

(转发)IOS高级开发~Runtime(三)

11.系统类的方法实现部分替换 - (void) methodExchange { Method m1 = class_getInstanceMethod([NSStringclass],@selector(lowercaseString)); Method m2 = class_getInstanceMethod([NSStringclass],@selector(uppercaseString)); method_exchangeImplementations(m1, m2); NSLog(

(转发)IOS高级开发~Runtime(四)

用C代替OC: #import <objc/runtime.h> #import <objc/message.h> #import <stdio.h> extern int UIApplicationMain (int argc,char *argv[],void *principalClassName,void *delegateClassName); struct Rect { float x; float y; float width; float height;

(转发)IOS高级开发~Runtime(一)

IOS高级开发-Runtime(一) IOS高级开发-Runtime(二) IOS高级开发-Runtime(三) IOS高级开发-Runtime(四) 一些公用类: @interface CustomClass : NSObject - (void) fun1; @end @implementation CustomClass - (void) fun1 { NSLog(@"fun1"); } @end @interface TestClass : NSObject @end @imp

iOS高级开发——CollectionView的cell中按钮的点击实现

在我刚初学iOS的时候,我就问一些大神,iOS开发中最难的哪些部分.有些人就说是自定义控件.UI和交互设计.那个时候我将信将疑,随着自己开发的深入,自己的确是深有体会.开发一款App产品,很大一部分时间是在和UI打交道.因为开发中很多功能是直接封装好的或者有现成模板可以用的,唯有UI是根据不同的App千变万化的.所以今天我们继续来研究iOS中比较高级的控件--UICollectionView,来实现cell中按钮的点击操作.该demo我已经提交到: https://github.com/chen

IOS高级开发~Runtime(四)

用C代替OC: #import <objc/runtime.h> #import <objc/message.h> #import <stdio.h> extern int UIApplicationMain (int argc,char *argv[],void *principalClassName,void *delegateClassName); struct Rect { float x; float y; float width; float height;

IOS高级开发~Runtime(二)

一些公用类: @interface ClassCustomClass :NSObject{ NSString *varTest1; NSString *varTest2; NSString *varTest3; } @property (nonatomic,assign)NSString *varTest1; @property (nonatomic,assign)NSString *varTest2; @property (nonatomic,assign)NSString *varTest3

ios高级开发,runtime(一)

创建公共类: @interface CustomClass : NSObject - (void) fun1; @end @implementation CustomClass -(void)fun1 { NSLog(@"fun1"); } @end @interface TestClass : NSObject @end @implementation TestClass @end 1.对象拷贝:id object_copy(id obj, size_t size) CustomCl