是否存在等效的“评估"?功能快速,就像在Matlab中一样?

问题描述:

如果没有,如何循环遍历IBOutlets(如temp1,temp2,temp3)并按顺序访问属性?

If not, how can I loop through IBOutlets (like temp1, temp2, temp3) and chance a property sequentially?

在matlab中,我将连接一个字符串并使用eval.

In matlab, I would concatenate a string and use eval.

for k = 1:3 
    eval(["temp",num2str(k)]);
end

我是Swift的新手,并且想通过IBOutlets做到这一点.像

I'm new to Swift, and want to do this with IBOutlets. Something like,

for(var k = 0, ++k, k==4) {
    eval(["self.temp",String(k),".backgroundcolor"]);
end

我知道eval函数是Matlab格式的,但是我只是在表达我想做的事情.

I know the eval function is in Matlab format, but I'm just expressing what I'd like to have done.

Swift是一种静态类型语言,因此无法在Matlab中实现"eval"之类的东西.您可以通过以下代码达到相同的行为:

Swift is a static type language, so that it's impossible to implement something like "eval" in Matlab. You can reach same behaviour with this code:

@IBOutlet var labels: [UILabel]!
override func viewDidLoad() {
    super.viewDidLoad()
    for label in labels {
        label.backgroundColor = UIColor.redColor()
    }
}

尽管可能可以使用运行时进行一些魔术操作,但是连接同一插座集合中的所有组件是最简单的方法.

Although probably it is possible to do some magic using the runtime, connecting all the components in the same outlet collection is the easiest way.