C class at run time from a text file?

Hi guys so I have an odd question. I want to create an Objective C classes 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.


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?

  • Sure. Totally possible.

    I would suggest starting with the Objective-C support in this as it includes a full-on Objective-C parser and code generator.


    see my github project cocoa-interprreter it does part of what you want.

    it takes a text file and compiles it at runtime .. and then runs the resulting executable using NSTask. It would be quite easy to change it so the binary is loaded into the own process using NSBundle

    https://github.com/Daij-Djan/cocoa-interpreter

    链接地址: http://www.djcxy.com/p/64718.html

    上一篇: 面板在渲染时间和性能方面的效率最高?

    下一篇: C类在运行时从文本文件?