2

I want to create an Objective-C application which lets you specify a class implementation at runtime.

I want the user to type some code (correctness of the code is out of scope for now). When the user is done i want to create a class of the typed code and use it in the application.

So i want to dynamically add code in runtime of the application. Is this possible? If so, how can i achieve this? If not, why not and are there any alternatives to create the same effect which i want to create?

Thanks.

Mats Stijlaart
  • 5,058
  • 7
  • 42
  • 58
  • 1
    FYI, you wouldn't be able to put this in the app store since it violates Apple's rules to be able to add code like that. – progrmr Sep 14 '11 at 04:06

3 Answers3

5

You can dynamically load classes at runtime, but to get there you'd first need to handle distributing a compiler, its compilation dependencies (headers, ...), setup its enviroment, etc.

Usually applications use scripting languages that are painlessly embeddable (Lua, Python, ...) or already available on the platform (JavaScript, AppleScript, ...).

Community
  • 1
  • 1
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
3

I'd check out F-Script. It's closer to smalltalk than Obj-C, but IMO it's closer to Obj-C than JavaScript or Lua :-)

Sophistifunk
  • 4,742
  • 4
  • 28
  • 37
1

On iOS devices, this isn't possible. On a Mac you could link against the clang+llvm libraries and use them to generate code into a buffer, then mprotect() the buffer to be executable, I believe.

Catfish_Man
  • 41,261
  • 11
  • 67
  • 84