本文介绍了如何访问变量名并将字符串更改为它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多图像,它们被命名为 image0、image1、image2...、image(x).

I have number of images and they are named as image0, image1, image2..., image(x).

我的问题是如何访问他们的名字,因为我想将它们全部放在数组中,例如:

My question is how can I access their name because I want to put all of them in array like:

@IBOutlet weak var image0: UIImageView!
@IBOutlet weak var image1: UIImageView!
@IBOutlet weak var image2: UIImageView!
@IBOutlet weak var image3: UIImageView!
@IBOutlet weak var image4: UIImageView!
@IBOutlet weak var image5: UIImageView!
...
@IBOutlet weak var image15: UIImageView!
    var counter: Int = 0
    var items :[[UIImageView]] = []
    for i in 0..<4{
        for j in 0..<4{
            items[i][j]=image(counter) //this line should be something that image0,1..x assign to the array
            counter += 1
        }
    }
}

推荐答案

我明白你在做什么.遗憾的是,变量名是在编译时计算的,所以这是不可能的.您能做的最好的事情是手动创建数组.

I see what you are trying to do. Sadly variable names are evaluated at compile time, so that's not possible. The best thing you can do is to manually create the array.

[[image0, image1, image2], [image3, image2, image3], ...]

这篇关于如何访问变量名并将字符串更改为它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 08:09