Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.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/12.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 需要随机排列一组图像和文字。Arc4random_均匀_Ios_Arrays_Swift_Arc4random - Fatal编程技术网

Ios 需要随机排列一组图像和文字。Arc4random_均匀

Ios 需要随机排列一组图像和文字。Arc4random_均匀,ios,arrays,swift,arc4random,Ios,Arrays,Swift,Arc4random,我正在做一个关于形状和文字的项目。我目前有两个数组。一个数组是图像数组,另一个数组只是描述第一个图像数组的一组字符串。我使用4个按钮来显示“字符串”数组中的4个项目。有人能告诉我如何使用arc4random,但也要确保我的4个按钮之一显示图像的名称 下面是我的数组的一个快速示例 var myShapes = ["circle", "square", "rectangle"] var images = ["circle.png", "square.png", "rectangle.png"]

我正在做一个关于形状和文字的项目。我目前有两个数组。一个数组是图像数组,另一个数组只是描述第一个图像数组的一组字符串。我使用4个按钮来显示“字符串”数组中的4个项目。有人能告诉我如何使用arc4random,但也要确保我的4个按钮之一显示图像的名称

下面是我的数组的一个快速示例

var myShapes = ["circle", "square", "rectangle"]

var images = ["circle.png", "square.png", "rectangle.png"] 
如何利用arc4Random,但确保每个数组中的一项始终相等

为函数添加信息以随机化我的图像

func randomImage() -> UIImage {

    let arrayCount = UInt32(myImages.count)
    let unsignedRandomNumber = arc4random_uniform(arrayCount)
    let randomNumber = Int(unsignedRandomNumber)



    return UIImage(named: myImages[randomNumber])!

}
下面是我如何做的随机文本

    let randomDisplayText = buttonDisplayText[Int(arc4random_uniform(UInt32(buttonDisplayText.count)))]

你能使用关联数组/字典吗?或者一个2d数值数组,它保存每个条目在
myShapes
images
中包含的数据?
var images:[String]{return myShapes.map{$0+“.png}
另外--告诉我们您计划如何使用
arc4random
对数组进行随机化会有所帮助。如果您只是计算一个随机键,您可以重用该键。@LeoDabus我认为这是一个公平的假设,即图像路径不会总是附加
.png
的描述?好的,谢谢您的评论。我相信我会的我必须按照字典的方法来完成这项工作。我正在试图弄清楚如何使用arc4random,让字典访问字符串和UIImage。我的代码目前出现了错误。下面是我迄今为止编写的函数。func randoImage()->UIImage{let index:Int=Int(arc4random_uniform(UInt32(myImages.count)))let value=Array(myImages.values)[index]let key=Array(myImages.keys)[index]return(key,value!)}您可以使用一个关联数组/字典吗?或者一个二维数值数组,它可以保存每个条目在
myShapes
images
中包含的数据吗?
var images:[String]{return myShapes.map{$0+“.png”}
另外--告诉我们您计划如何使用
arc4random
对数组进行随机化会有所帮助。如果您只是计算一个随机键,您可以重用该键。@LeoDabus我认为这是一个公平的假设,即图像路径不会总是附加
.png
的描述?好的,谢谢您的评论。我相信我会的我必须按照字典的方法来完成这项工作。我正在试图弄清楚如何使用arc4random,让字典访问字符串和UIImage。我的代码目前出现了错误。下面是我迄今为止编写的函数。func randoImage()->UIImage{let index:Int=Int(arc4random_uniform(UInt32(myImages.count)))let value=Array(myImages.values)[index]let key=Array(myImages.keys)[index]return(key,value!))