Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create an Objective-C class at run time from a text file?

I want to create an Objective C class at runtime from a file. So for example I have an objective c application, I want to then point it at a text file (or an .h .m pair, whatever works) and then have the application parse the text file and create the class at runtime (Class no object). Of course I would write the parser and all of that stuff I just want to know if its possible. I read these two articles:

http://www.mikeash.com/pyblog/friday-qa-2010-11-6-creating-classes-at-runtime-in-objective-c.html

http://www.mikeash.com/pyblog/friday-qa-2010-11-19-creating-classes-at-runtime-for-fun-and-profit.html

Which shows how to make an objective C class at runtime, but its being done using C functions which were defined at compile time. If I can find a way to do the same thing using strings to define the functions that would be perfect, because then I don't have to define them at compile time.

like image 924
Samuel Rosen Avatar asked Oct 11 '12 06:10

Samuel Rosen


1 Answers

That's what called reflective programming. I guess there's no code evaluation support for Obj-C since it's not a scripting language, so the reflection concept for Obj-C is quietly limited. Plus, at run-time the compiler already translate the code into Obj-C clang code and it's a very time-consuming job just to reverse-translate the bytecode and recompile it again

For Obj-C reflection you can refer to these answers

  • Build a class : Create objective-c class instance by name?
  • Implement a method : Objective-C, how can i hook up a method in another class
  • Change class for an object : Objective-C: How to change the class of an object at runtime?
like image 190
stack underflow Avatar answered Nov 18 '22 06:11

stack underflow