一些公用類:
@interfaceCustomClass :NSObject
- (void) fun1;
@end
@implementationCustomClass
- (void) fun1
{
NSLog(@"fun1");
}
@end
@interfaceTestClass :NSObject
@end
@implementationTestClass
@end
別忘記引入庫:#include
1、對象拷貝:idobject_copy(idobj, size_t size)
- (void) copyObj
{
CustomClass*obj = [CustomClassnew];
NSLog(@"%p", &obj);
idobjTest =object_copy(obj,sizeof(obj));
NSLog(@"%p", &objTest);
[objTestfun1];
}
打印結果:
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、對象釋放idobject_dispose(idobj)
- (void) objectDispose
{
CustomClass*obj = [CustomClassnew];
object_dispose(obj);
[objrelease];
[objfun1];
}
打印結果:程序crash
malloc: *** error for object 0x758e6d0: pointer being freed was not allocated
3、更改對象的類/獲取對象的類
Class object_setClass(idobj, Class cls) ?/
Class object_getClass(idobj)
- (void) setClassTest
{
CustomClass*obj = [CustomClassnew];
[objfun1];
ClassaClass =object_setClass(obj, [CustomClassOtherclass]);
//obj對象的類被更改了swap the isa to an isa
NSLog(@"aClass:%@",NSStringFromClass(aClass));
NSLog(@"obj class:%@",NSStringFromClass([objclass]));
[objfun2];
}
- (void) getClassTest
{
CustomClass*obj = [CustomClassnew];
ClassaLogClass =object_getClass(obj);
NSLog(@"%@",NSStringFromClass(aLogClass));
}
4、獲取對象的類名constchar*object_getClassName(idobj)
- (void) getClassName
{
CustomClass*obj = [CustomClassnew];
NSString*className = [NSStringstringWithCString:object_getClassName(obj)encoding:NSUTF8StringEncoding];
NSLog(@"className:%@", className);
}
5、給一個類添加方法
BOOLclass_addMethod(Class cls,SELname,IMPimp,
constchar*types)
/**
*一個參數
*
*/
intcfunction(idself,SEL_cmd,NSString*str) {
NSLog(@"%@", str);
return10;//隨便返回個值
}
- (void) oneParam {
TestClass*instance = [[TestClassalloc]init];
//方法添加
class_addMethod([TestClassclass],@selector(ocMethod:), (IMP)cfunction,"i@:@");
if([instancerespondsToSelector:@selector(ocMethod:)]) {
NSLog(@"Yes, instance respondsToSelector:@selector(ocMethod:)");
}else
{
NSLog(@"Sorry");
}
inta = (int)[instanceocMethod:@"我是一個OC的method,C函數實現"];
NSLog(@"a:%d", a);
}
/**
*兩個參數
*
*/
intcfunctionA(idself,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([instancerespondsToSelector:@selector(ocMethodA::)]) {
NSLog(@"Yes, instance respondsToSelector:@selector(ocMethodA::)");
}else
{
NSLog(@"Sorry");
}
inta = (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)