是否存在等效的“评估"?功能快速,就像在Matlab中一样? [英] Is there an equivalent "eval" function for swift, like there is in Matlab?

查看:68
本文介绍了是否存在等效的“评估"?功能快速,就像在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.

这篇关于是否存在等效的“评估"?功能快速,就像在Matlab中一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆