除了数组的最后一个元素外,一切都运行良好,我得到了致命错误:当我点击SnapButton时,数组索引超出范围。我试着添加:
//var indexA = getRandomIntFromArray(cardNamesArray)
//var indexB = getRandomIntFromArray(cardNamesArray2)
但我得到的不能用于类型View Controller不确定这意味着什么
var firstRandomNumber = Int()
var secondRandomNumber = Int()
class ViewController: UIViewController {
@IBOutlet weak var FirstCardImageView: UIImageView!
@IBOutlet weak var SecondCardImageView: UIImageView!
@IBOutlet weak var PlayRoundButton: UIButton!
@IBOutlet weak var BackgroundImageView: UIImageView!
@IBOutlet weak var playerScoreLabel: UILabel!
var playerScore: Int = 0
var cardNamesArray = ["acorn", "angry","apple", "rainbow", "sad","boots", "heart", "pumpkin","chestnuts"]
var cardNamesArray2 = ["bellota", "enfadado","manzana", "arcoiris","triste","botas","corazon", "calabaza", "castana"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func playRoundTapped(sender: UIButton) {
firstRandomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(cardNamesArray.count)
FirstCardImageView.image = UIImage(named: cardNamesArray[firstRandomNumber])
secondRandomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(cardNamesArray2.count)
SecondCardImageView.image = UIImage(named: cardNamesArray2[secondRandomNumber])
}
func getRandomIntFromArray(array: [String]) -> Int {
return GKRandomSource.sharedRandom().nextIntWithUpperBound(cardNamesArray.count)
}
//var indexA = getRandomIntFromArray(cardNamesArray)
//var indexB = getRandomIntFromArray(cardNamesArray2)
@IBAction func SnapButtonTapped(sender: UIButton) {
if cardNamesArray.count > 0 {
if firstRandomNumber == secondRandomNumber {
cardNamesArray.removeAtIndex(firstRandomNumber)
cardNamesArray2.removeAtIndex(secondRandomNumber)
firstRandomNumber = getRandomIntFromArray(cardNamesArray)
secondRandomNumber = getRandomIntFromArray(cardNamesArray2)
//on the last element of the array when I click on SnapButtonTapped I get fatal error: Array index out of range
FirstCardImageView.image = UIImage(named: cardNamesArray[firstRandomNumber])
SecondCardImageView.image = UIImage(named: cardNamesArray2[secondRandomNumber])
print(cardNamesArray)
} else {
print("no cards in array")
}
}
}
}
答案 0 :(得分:1)
您想要从卡中删除图像吗?
如果答案是肯定的,只需将卡片的图像更改为其他内容:
if firstRandomNumber == secondRandomNumber {
//print("index match")
//self.playRoundTapped(self.PlayRoundButton) <--- I have tried to recall the playRoundTapped method but it doesn't remove the element and goes crazy
cardNamesArray.removeAtIndex(firstRandomNumber)<----how can I apply this to a UIImage?
cardNamesArray2.removeAtIndex(secondRandomNumber)
FirstCardImageView.image = UIImage(named: "someDefaultImage")
SecondCardImageView.image = UIImage(named: "someDefaultImage")
}
答案 1 :(得分:1)
您可以在Playground上运行此操作以了解逻辑。
import UIKit
import GameKit
var array1 = ["one", "two"]
var array2 = ["one", "two"]
// Create a helper method to get random int
func getRandomIntFromArray(array: [String]) -> Int {
return GKRandomSource.sharedRandom().nextIntWithUpperBound(array1.count)
}
var indexA = getRandomIntFromArray(array1)
var indexB = getRandomIntFromArray(array2)
// At snapButtonTapped
// Check the array count first so that you will not get index out of range.
if array1.count > 0 {
if indexA == indexB {
// First remove index first
array1.removeAtIndex(indexA)
array2.removeAtIndex(indexB)
// Assign to new random index
indexA = getRandomIntFromArray(array1)
indexB = getRandomIntFromArray(array2)
// After assigned a new random index, set image base on that index
// FirstCardImageView.image = UIImage(named: array1[indexA])
// SecondCardImageView.image = UIImage(named: array2[indexB])
}
} else {
print("Finished, all array was removed") // Do anything here such as set empty image for FirstCardImageView & SecondCardImageView or success alert.
}
答案 2 :(得分:0)
您的数组似乎正在跟踪字符串,而不是图像。
这一行
SecondCardImageView.image = UIImage(named: secondCardString)
正在创建基于String的图像,该图像未与String引用关联或连接。
如果你想管理图像,你需要保留一组图像而不是字符串。
或者,您可以使用词典Swift Collection Types