11, the System class method realizes partial substitution
-(void) Methodexchange {
Method m1 = Class_getinstancemethod ([NSString class], @selector (lowercasestring));
Method m2 = Class_getinstancemethod ([NSString class], @selector (uppercasestring));
Method_exchangeimplementations (M1, M2);
NSLog (@ "%@", [@ "AAAAAAAA" lowercasestring]);
NSLog (@ "%@", [@ "AAAAAAAA" uppercasestring]);
}
Operation Result:
2014-10-20 14:48:17.584 runtimetest[14921:303] Aaaaaaaa
2014-10-20 14:48:17.586 runtimetest[14921:303] Aaaaaaaa
12. Partial substitution of the method implementation of the custom class
-(void) JustLog1 {
NSLog(@ "JustLog1");
}
-(void) justLog2 {
NSLog(@ "justLog2");
}
-(void) methodsetimplementation {
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. Coverage System method
IMP Cfuncpointer;
IMP CFuncPointer1;
IMP CFuncPointer2;
nsstring* customuppercasestring (ID self,SEL_cmd) {
printf(" What really works is this function customuppercasestring\r\n");
nsstring *string = cfuncpointer (self,_cmd);
return string;
}
nsarray* customcomponentsseparatedbystring (ID self,SEL_cmd,nsstring *str) {
printf(" What really works is this function customisequaltostring\r\n");
return cFuncPointer1 (self,_cmd, str);
}
does not work, to explain
BOOL Customisequaltostring (ID self,SEL_cmd,nsstring *str) {
printf(" What really works is this function 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,"[email protected]:@");
}
14. automatic serialization (GO)
#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;
}
iOS advanced development, Runtime (iii)