简体   繁体   English

如何比较数组中的UIImages?

[英]How to compare UIImages in arrays?

I'm making an application where it would be nice to be able to compare values inside one array/set. 我正在开发一个应用程序,它能够比较一个数组/一组内部的值会很好。

Let's say I have constants and an array like this: 假设我有一个常量和一个像这样的数组:

let blueImage = UIImage(named: "blue")
let redImage = UIImage(named: "red")

button.setImage(blueImage, forState: .Normal)
button2.setImage(redImage, forState: .Normal)
button3.setImage(blueImage, forState: .Normal)

var imageArray:Array<UIImage> = [button.currentImage, button2.currentImage, button3.currentImage]

Is it then possible to check/compare the values in my array and replace the red images with the blue ones. 然后可以检查/比较我的数组中的值并将红色图像替换为蓝色图像。

More specifically is there a way I can check if 2/3 of the images in the array contains a specific image( blueImage ), and then replace the last value( redImage ) with ( blueImage ) so that all has the same picture. 更具体地讲是有办法我可以检查是否在阵列中的图像的2/3包含特定的图像( blueImage ),然后替换的最后一个值( redImage )与( blueImage ),使得所有具有相同的画面。

I guess you could filter the array with something along these lines: 我猜您可以按照以下方式过滤数组:

let filteredArray = filter(imageArray) { $0 == blueImage }

and then run a count. 然后进行计数。

You could also iterate over your array: 您还可以遍历数组:

let countBlue = 0

for i in 0..<imageArray.count {

    if imageArray[i] == blueImage {
        countBlue ++
    }
}

To replace an element: 替换元素:

imageArray[2] = blueImage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM