在Objective-C 2.0中有类似Matlab的eval语句吗? [英] Is there something like Matlab's eval statement in Objective-C 2.0?
问题描述
我是Objective-C的新手,我正在寻找一个像我在Matlab中使用的 eval
语句。
I am new to Objective-C and I am looking for an eval
statement like I have used in Matlab.
如果您对此不熟悉,可以构建一个字符串,然后 eval
该字符串,对待它就像是一行代码。
If you are not familiar with this, you can build a character string and then eval
that string, which treats it like it is a line of code.
这是一个例子,你想根据变量foo改变一系列4个按钮之一的背景颜色where = 3,你的按钮名为button1,button2等。
Here is a example where you would want to change the background color of one of a series of 4 buttons based on a variable foo which = 3 and you buttons would be named button1, button2 etc.
NSString* buttonEval = [[NSString alloc] initWithFormat:@"[button%d setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];", foo]
Is有一个声明会评估这个字符串,好像它是一行代码吗?
Is there a statement the will evaluate this string as if it was a line of code?
推荐答案
否;虽然Objective-C是一种动态类型语言,但它仍然是一种编译语言,而不是像Javascript或PHP这样的语言,不是解释语言。
No; although Objective-C is a dynamically typed language, it is still a compiled language, and not an interpreted one, unlike a language such as Javascript or PHP, for example.
对于上面的示例,您可以使用数组存储指向 UIButton
实例的指针:
For the above example, you could use an array to store pointers to your UIButton
instances:
buttonArray = [[NSMutableArray alloc] init];
...
UIButton *aButton; //Reference to a UIButton instance
[buttonArray addObject:aButton];
稍后检索指向 UIButton
的指针你要调用方法。
And later retrieve a pointer to the UIButton
that you want to call the method on.
[[buttonArray objectAtIndex:foo] setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
这篇关于在Objective-C 2.0中有类似Matlab的eval语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!