Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift:图像名称中的数组_Ios_Arrays_Swift_Uiimage - Fatal编程技术网

Ios Swift:图像名称中的数组

Ios Swift:图像名称中的数组,ios,arrays,swift,uiimage,Ios,Arrays,Swift,Uiimage,我有一批名为pattern35和pattern7的图像。我的UIImage会更改,它需要是一个随机图像。共有84种模式。我有一个随机数数组。我尝试了这一点,但得到的错误是“array”是一个未解析的标识符 patternRoom.image = UIImage(named: "pattern\(array[0])") 听起来您的数组在一个函数中定义时存在作用域问题,而该函数不是使用它的函数。将下面的代码作为一个可用于其他函数的声明的例子: class SampleClass { let

我有一批名为pattern35和pattern7的图像。我的UIImage会更改,它需要是一个随机图像。共有84种模式。我有一个随机数数组。我尝试了这一点,但得到的错误是“array”是一个未解析的标识符

patternRoom.image = UIImage(named: "pattern\(array[0])")

听起来您的数组在一个函数中定义时存在作用域问题,而该函数不是使用它的函数。将下面的代码作为一个可用于其他函数的声明的例子:

class SampleClass {
    let array: [Int] = [1, 2, 3]

    func showThem() {
        for index in 0...2 {
            println("Value is \(array[index])")
        }
    }
}

let sc: SampleClass = SampleClass()
sc.showThem()

(操场非常适合在与可能更复杂的“生产”代码隔离的情况下尝试这种事情。)

您可以这样做:

patternRoom.image = UIImage(named: "pattern\(arc4random_uniform(84))")

与你发布的行的位置相比,你在哪里声明了你的随机数数组?@PhillipMills在一个单独的func中,但在函数中定义的同一个类中,如
func f(){let array=[1,2,3]}
?在这种情况下,这就是问题所在。