I have made an array of UIImagesViews and I have made a code to assign them random pictures. I would like to make an if-statement if more than or 5 pictures have the same picture shown.
var images: [UIImageView] = [Image1, Image2, Image3, Image4, Image5, Image6, Image7, Image8, Image9]
The different pictures are named card1, card2, card3
let firstRandomNumber = arc4random_uniform(3) + 1
let firstRandomString:String = String(format: "card%i", firstRandomNumber)
self.Image1.image = UIImage(named: firstRandomString)
let secondRandomNumber = arc4random_uniform(3) + 1
let secondRandomString:String = String(format: "card%i", secondRandomNumber)
self.Image2.image = UIImage(named: secondRandomString)
All images have their own specific code like the ones above. Now, as I said, I would like to make an if
-statement or switch
-case if five or more of the Images have the same picture shown.
Edit: Here is the updated code;
let imageNames:NSArray = [firstRandomString, secondRandomString, thirdRandomString, fourthRandomString, fifthRandomString, sixthRandomString, seventhRandomString, eighthRandomString, ninthRandomString]
let maxRepeating = imageNames
.map {img in imageNames.reduce(0) { $1 == img ? $0 + 1 : $0} }
.reduce(0){ $1 > $0 ? $1 : $0 }
The code only has one error at .map {img in imageNames.reduce(0) { $1 == img ? $0 + 1 : $0} }
saying;
Binary operator '==' cannot be applied to two 'Element' (aka 'AnyObject') operands
Edit: Here is another code I have tried from Christopher Kevin;
var correct = 0
func checkImageDuplication() {
let imageArray : NSArray = [firstRandomString, secondRandomString, thirdRandomString, fourthRandomString, fifthRandomString, sixthRandomString, seventhRandomString, eighthRandomString, ninthRandomString]
let imageDataArray = imageArray.map { (image) -> NSData in
return UIImagePNGRepresentation(image as! UIImage)!
}
let countSet = NSCountedSet(array: imageDataArray)
for imageData in imageDataArray {
let count = countSet.countForObject(imageData)
if count > 5 {
correct = 5
}
}
}
@IBAction func PlayerPRESSED(sender: AnyObject) {
if correct == 5 {
view.backgroundColor = UIColor.greenColor()
}
}
I tried the code but it will not execute the action. (view.backgroundColor) Anyone have any Idea what is wrong with this code?