7

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.

peterh
  • 11,875
  • 18
  • 85
  • 108
Samuel Rosen
  • 183
  • 1
  • 2
  • 5
  • Tut. You want to re-implement clang and the linker? Do you know how much work that is? – trojanfoe Oct 11 '12 at 07:07
  • You should be able to invoke clang to compile your source to generate a dynamic library/bundle that you could then load using NSBundle – nielsbot Oct 11 '12 at 08:04
  • there's not much point to doing this for objective-c files--for that you should just use clang (or the clang source)... If you are interested in creating a custom language, that makes more sense... – nielsbot Oct 11 '12 at 08:09
  • Sounds a bit like an academic exercise to me (and nothing wrong with that), but you might want to take a look at http://cappuccino.org . Being based on JavaScript, it should be able to do run-time evaluation of quite a few things. Might need some tweaking, though. – Monolo Oct 11 '12 at 08:25
  • I don't think this is possible. This means you would have to compile the Classes while running. Also, this sounds VERY VERY buggy. – IluTov Oct 11 '12 at 08:00
  • This sounds really expensive as far as memory and cpu usage goes, but still - it is an excellent question :) – LJ Wilson Dec 24 '12 at 10:15

3 Answers3

2

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

Community
  • 1
  • 1
stackunderflow
  • 1,492
  • 1
  • 23
  • 53
1

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.

bbum
  • 162,346
  • 23
  • 271
  • 359
1

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

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135