iOS,是否可以从文件中读取objective - C代码并在程序的特定点执行?

时间:2023-02-06 10:32:49

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.

我有一堆绘图代码(原始绘图,主要是bezier路径绘图调用),它们存储在几个文件中,绘制代码可以运行到大约几百行。我想知道是否可以从一个文件中读取这个代码,并在我的程序中的某个特定点执行它(可能是一个UIView的绘制方法),就像我们在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!

我想用这段代码来绘图,但是我不想把绘图代码复制到我正在使用的UIView类中,因为维护起来很麻烦,很难读懂,而且我有很多这样的文件!

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!

有什么想法/帮助感激!

2 个解决方案

#1


1  

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).

我认为您真正想要的是一个类别(https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html)。你可以在UIView上创建一个类别,其中包含所有的绘图代码,然后将这个类别导入需要它的视图子类,并在需要它的地方调用方法。我很确定苹果不会让你在iPhone上编译代码(显然着色器是个例外)。

#2


0  

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.

不可能读取代码格式文件的行并使其在运行时可执行。你可以子类化你的UIView类包含绘图代码或者你也可以创建这个类的类别然后你可以根据你的需要调用那个代码。

#1


1  

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).

我认为您真正想要的是一个类别(https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html)。你可以在UIView上创建一个类别,其中包含所有的绘图代码,然后将这个类别导入需要它的视图子类,并在需要它的地方调用方法。我很确定苹果不会让你在iPhone上编译代码(显然着色器是个例外)。

#2


0  

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.

不可能读取代码格式文件的行并使其在运行时可执行。你可以子类化你的UIView类包含绘图代码或者你也可以创建这个类的类别然后你可以根据你的需要调用那个代码。