无夜之星辰 无夜之星辰 - 2 months ago 27
Objective-C Question

Can I get the source code from the .m file?

I want to get the source code from .m file so I use this way:

NSString *path = [[NSBundle mainBundle] pathForResource:@"ViewController" ofType:@".m"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", content);


However the console print "null".

I wonder if there any way we can use to get .m file's content even if use private method.

Answer Source

If you're copying the actual source file inside your application's bundle and including it, then sure, that'll work, although I don't know why you'd want to do that.

If you're wondering if you can decompile a binary file back to Objective-C code, then no, you can't do that—at least not simply. If you have good enough skill with assembly language, of course, you can read a disassembly of the code, figure out what the original Objective-C did, and use that knowledge to recreate it.