使用 UIImage 从数组中删除索引

Posted

技术标签:

【中文标题】使用 UIImage 从数组中删除索引【英文标题】:remove index from array with UIImage 【发布时间】:2016-03-04 16:04:41 【问题描述】:

除了数组的最后一个元素外,一切正常,我收到致命错误:单击 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")

        

    




【问题讨论】:

【参考方案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.

【讨论】:

感谢您查看此内容,我收到一条错误消息“当我分配 IndexA = getRandomIntFromArray(array1) 时,实例成员 'cardNamesArray 不能用于类型 'ViewController' 欢迎,很高兴你得到它! 嘿,看看我原来的帖子,我又收到了“不能用于 ViewController 类型”的消息。不知道这是什么意思【参考方案2】:

您想从卡片中删除图像吗?

如果答案是肯定的,只需将卡片的图像更改为其他内容:

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")
         

【讨论】:

而不是“someDefaultImage”如何将其更改为数组中的下一个图像? 你已经有了你当前的图像索引,它是 firstRandomNumber 和 secondRandomNumber。因此,只需从您的图像数组中获取下一个索引处的图像,例如 let image1Name = myImagesArray[firstRandomNumber+1] let image2Name = myImagesArray[secondRandomNumber+1] 嘿,我将代码放在 removeAtIndex 下,但告诉我用 _ let FirstCardImageView = cardNamesArray[firstRandomNumber+1] 替换“let” 当你不需要某些函数的结果时,你使用 _。发布您的代码,以便更轻松地查看您所做的事情 在我的原始帖子中查看我更新的代码。我有两个问题第一个;卡片更改为下一个匹配对应该是随机的。第二个我得到数组索引超出范围。【参考方案3】:

您的数组似乎正在跟踪字符串,而不是图像。

这一行

 SecondCardImageView.image = UIImage(named: secondCardString)

正在基于字符串创建图像,该字符串未关联或连接字符串引用。

如果要管理图像,则需要保留图像数组而不是字符串。

您也可以使用字典Swift Collection Types

【讨论】:

以上是关于使用 UIImage 从数组中删除索引的主要内容,如果未能解决你的问题,请参考以下文章

不使用索引号从数组中删除[重复]

使用花哨的索引从 Numpy 数组中查找和删除全零列

ruby 使用索引从数组中删除值

ruby 使用索引从数组中删除值

如何使用flutter从firestore中删除/更新数组对象中的指定索引

firebase 按索引从字段数组中删除对象