2

I need to ask, how can I convert a string containing a piece of code to actual runnable code:

  int x = 7;
  int y = 9;
  NSString *myCode = @"if(x > y) return x; else return y;";

I need to be able to run myCode as actual Objective C code. The example given is a dummy example just to illustrate the idea.

halfer
  • 19,824
  • 17
  • 99
  • 186
coder
  • 5,200
  • 3
  • 20
  • 45
  • When you say "run" do you mean that you will parse and evaluate the expression or do you mean to convert it to machine code and have it executed? – Frank C. Oct 08 '16 at 10:19
  • I need to be able to execute it – coder Oct 08 '16 at 10:37
  • You could call (existing) selectors based on string input, you could pass them some parameters based on string input, but you can not "compile and run some arbitrary objective-c code on the fly". Unless you are thinking of your own predefined scripting rules & parser: from your question it doesn't look like you had that in mind. – Rok Jarc Oct 08 '16 at 11:41
  • what I need to do, is to allow the user at any time to write a pseudocode of some rules and to be able to change the pseudocode into actual code and to be able to apply it. I know I can have already set rules and based on the input to choose a specified rule, but Im thinking of smg wider. Thank you for your suggestion. – coder Oct 09 '16 at 04:46

1 Answers1

0

For the objective-c is not Interpretive language, so it can't achieve by API.

Maybe can be accomplish by following steps

  1. Use JavaScript to wrap c code

  2. Use JSPatch[https://github.com/bang590/JSPatch] to run the code

Snail
  • 3,046
  • 1
  • 9
  • 12