iOS , Is it possible to read objective C code from a file and execute it at a specific point in the program?

StackOverflow https://stackoverflow.com/questions/19240868

Domanda

I have a bunch of drawing code (primitive drawing, mostly bezier path drawing calls) thats stored in a couple of files and the draw code runs to about a few hundred lines. I wanted to know if its possible to read this code from a file and execute it at a particular point in my program (maybe a draw method of a UIView), much like what we do with shaders in openGL 2.0.

I want to be able to use this code to do my drawing but i dont want to copy the drawing code into the UIView class I'm using because it becomes cumbersome to maintain and hardly readable, plus i have many such files!

I know that i can read the contents of a file using existing frameworks, but is there something that will allow me to execute the read contents of the file where i need this drawing code to run?

Any ideas/help is appreciated!

È stato utile?

Soluzione

I think you you really want is a category (https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html). You would make a category on UIView for example with all your drawing code in it, then import the category to your view subclasses that need it and call the method where you need it. I'm pretty sure Apple won't let you compile code on an iPhone (shaders are obviously an exception).

Altri suggerimenti

This is not possible to read lines of code form file and make it executable at run time. You can sub-class your UIView Class that contains drawing code or You can also make Category of this class then you can call that code according to you requirement.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top