I can get a method’s implementation by:
IMP imp = [self methodForSelector:@selector(foo)];
and I know IMP is basically a function pointer.
Then Can I get the code that pointed?Means save it with NSData or char* or something.
Then I can save out of program and reload it dynamically from file.
thanks.
You need to think carefully about what it is that you want to do here and tell us the your motivation. You are correct that the selector is essentially a function pointer. It is possible for you to copy the bytes used to encode the instructions making up the function but there is little benefit for you to do so.
Carelessly relocating a function will likely not allow it to execute properly. For example, assume that there is a relative branch within your function which reaches outside of the code that you have copied. The target address is resolved/calculated based on the value of the current
pc(program counter) and will resolve incorrectly if you simply copy the code.