Executing code in an NSString

Discussion in 'Mac Programming' started by qwertywelch, Dec 15, 2010.

  1. qwertywelch macrumors newbie

    Joined:
    Oct 16, 2010
    #1
    Hello!

    I am retreiving a text file from my web server and loading its contents into an NSString called "latestCode". NSLog tells me that it is getting the right code, but now how do I execute it?
     
  2. dantherevelator macrumors regular

    Joined:
    Oct 8, 2007
  3. qwertywelch thread starter macrumors newbie

    Joined:
    Oct 16, 2010
    #3
    Objective C
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    While objective-c does allow you to dynamically load frameworks, it does not allow for dynamic execution of arbitrary code. It is not an interpreted language, code must be compiled into a binary object.

    -Lee
     
  5. qwertywelch thread starter macrumors newbie

    Joined:
    Oct 16, 2010
    #5
    how would I go about compiling one block of code that can be loaded from a web server & executed?
     
  6. dantherevelator macrumors regular

    Joined:
    Oct 8, 2007
    #6
    Well, it's not straightforward, and probably impossible on iOS. Objective C must be compiled to run, and I assume this is raw source code, yes?

    If it's an OS X app, and you're feeling adventurous, you might look into something like f-script.
     
  7. qwertywelch thread starter macrumors newbie

    Joined:
    Oct 16, 2010
    #7
    It's an OS X app. F-script looks like just what I need, thanks!
     
  8. lee1210, Dec 15, 2010
    Last edited: Dec 15, 2010

    lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    I can point you to objc_loadModules that you can search for... I suppose you could use system or NSTask to invoke gcc, then load the object file with the function mentioned above. This seems quite involved though. Perhaps you could give some background and another method such as distributing a bundle, loading with NSBundle, then executing the new code (though this is still involved) could be suggested? Assuming your end users have a compiler and libraries installed is ill-advised.

    -Lee

    EDIT: Hadn't really looked at f-script. Looks fancy, and if that will do the job for you it's probably better than the options I mentioned.
     
  9. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #9
    You could JIT-compile ObjC code in-process using clang's libraries.
     

Share This Page