0

In Ruby, I was able to execute strings as code like

eval("print 5")

Is it possible to do exactly the same in Objective-C?

Saturn
  • 17,888
  • 49
  • 145
  • 271

1 Answers1

3

No. Objective-C is a compiled language. To evaluate strings like that, there would have to be a compiler embedded in the runtime, and there isn't.

There is a limited form of executing strings using methods like-performSelector and valueForKey: and NSInvocation.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • So, if i want to make an app for compiling co code from iOS app, what would be possible best way ? Already there are many apps on appstore. – Jamshed Alam Oct 16 '16 at 11:18
  • You either have to include an interpreter or a compiler in your app. Or you can probably leverage the Webkit Javascript interpreter, if you don't mind Javascript. – JeremyP Oct 17 '16 at 13:54
  • So, can i compile my C code with a local JS file (which return eval result) from app ? or i have to use it from server ? please, elaborate or give me some link to get help. I am going to dev an app regarding this function. – Jamshed Alam Oct 17 '16 at 15:45
  • @JamshedAlam It would be better if you authored a question rather than ask in the comments here, then more people will see it and answers can be written more easily. – JeremyP Oct 19 '16 at 08:57