Ios 使用UIImage从数组中删除索引
除了在数组的最后一个元素上,一切都很好,我得到了一个致命的错误:当我单击SnapButton时,数组索引超出了范围。我试图补充:Ios 使用UIImage从数组中删除索引,ios,arrays,swift,uiimage,Ios,Arrays,Swift,Uiimage,除了在数组的最后一个元素上,一切都很好,我得到了一个致命的错误:当我单击SnapButton时,数组索引超出了范围。我试图补充: //var indexA = getRandomIntFromArray(cardNamesArray) //var indexB = getRandomIntFromArray(cardNamesArray2) 但我得不能在类型视图控制器上使用,我不确定这意味着什么 var firstRandomNumber = Int() var secondRandomNum
//var indexA = getRandomIntFromArray(cardNamesArray)
//var indexB = getRandomIntFromArray(cardNamesArray2)
但我得不能在类型视图控制器上使用,我不确定这意味着什么
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")
}
}
}
}
看起来您的数组正在跟踪字符串,而不是图像 这条线
SecondCardImageView.image = UIImage(named: secondCardString)
正在基于字符串创建图像,该字符串未与字符串引用关联或连接
如果要管理图像,需要保留图像数组而不是字符串
或者,如果数组跟踪的是字符串,而不是图像,则可以使用字典 这条线
SecondCardImageView.image = UIImage(named: secondCardString)
正在基于字符串创建图像,该字符串未与字符串引用关联或连接
如果要管理图像,需要保留图像数组而不是字符串
或者,您可以使用字典要从卡中删除图像吗 如果答案是肯定的,只需将卡的图像更改为其他图像:
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")
}
如果firstRandomNumber==secondRandomNumber{
//打印(“索引匹配”)
//self.playRoundTapped(self.PlayRoundButton)要从卡中删除图像吗
如果答案是肯定的,只需将卡的图像更改为其他图像:
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")
}
如果firstRandomNumber==secondRandomNumber{
//打印(“索引匹配”)
//self.playRoundTapped(self.PlayRoundButton)你可以在操场上运行这个程序来理解逻辑
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.
}
你可以在操场上运行这个来理解逻辑
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.
}
如何将其更改为数组中的下一个图像而不是“someDefaultImage”?您已经有了当前图像索引,它是firstRandomNumber和secondRandomNumber。因此,只需从图像数组中获取下一个索引处的图像,类似于让image1Name=myImagesArray[firstRandomNumber+1]让image2Name=myImagesArray[secondRandomNumber+1]嘿,我把代码放在removeAtIndex下面,但告诉我用let FirstCardImageView=cardNamesArray[firstRandomNumber+1]替换“let”
当你不需要某个函数的结果时,你可以使用uu。发布你的代码,让你更容易看到你做了什么。看一下我在原始帖子中更新的代码。我发现两个问题:第一个问题;换成下一个匹配对的卡应该是随机的。第二个问题我得到数组索引超出范围。而不是“someDefaultImage”如何将其更改为数组中的下一个图像?您已经有了当前图像索引,它是firstRandomNumber和secondRandomNumber。因此,只需从图像数组中获取下一个索引处的图像,类似于让image1Name=myImagesArray[firstRandomNumber+1]让image2Name=myImagesArray[secondRandomNumber+1]嘿,我把代码放在removeAtIndex下面,但告诉我用let FirstCardImageView=cardNamesArray[firstRandomNumber+1]替换“let”
当您不需要某个函数的结果时,您可以使用uu。发布您的代码以便于查看您所做的事情。请查看我在原始帖子中更新的代码。我遇到两个问题:第一个问题;换成下一个匹配对的卡应该是随机的。第二个问题我的数组索引超出了范围。感谢您查看此问题,我收到一条错误消息“当我分配IndexA=getRandomIntFromArray(array1)时,实例成员'cardNamesArray不能用于类型'ViewController'欢迎,很高兴你收到它!嘿,看看我的原始帖子,我得到的是“不能用于类型ViewController”“再次发送消息。不确定这意味着什么感谢您的关注,我收到了一条错误消息”当我分配IndexA=getRandomIntFromArray(array1)时,实例成员'cardNamesArray不能用于类型'ViewController'欢迎,很高兴您收到它!嘿,看看我的原始帖子,我得到的是“不能用于类型ViewController”“请再说一遍,我不知道这是什么意思