通过连接到按钮的标识符对一系列项目进行洗牌......我哪里出错了?

Shuffling an array of items by the identifier connected to the buttons...Where am I going wrong?

我得到了一个应用程序,它包含 9 个按钮和一个显示与水果关联的文本的标签。

我创建了一个包含名称、对象和标识符的结构。我最初创建了一个 Outlet 集合,但无法弄清楚如何将按钮连接到初始化的标识符变量,然后我为所有按钮创建了单独的 outlets。

游戏从头到尾运行完美 - 我现在的问题是当我想回复游戏时,我想重置游戏但打乱文本标签。所以每次用户按下重播都会弹出一个新的水果名称。

有人能给我指出正确的方向吗?我如何才能进入标识符并在我单击它时随机播放图像。

谢谢

import Foundation

struct Fruitness
{
    var fruit = [Fruit]()
    
    var fruitOptions = [

        Fruit(identifier: 1, fruit:"", name: "KIWI"),
        Fruit(identifier: 2, fruit:"",name: "APPLE"),
        Fruit(identifier: 3, fruit:"", name: "PEAR"),
        Fruit(identifier: 4, fruit:"", name: "ORANGE"),
        Fruit(identifier: 5,fruit:"", name: "STRAWBERRY"),
        Fruit(identifier: 6,fruit:"", name: "WATERMELON"),
        Fruit(identifier: 7,fruit:"", name: "GRAPES"),
        Fruit(identifier: 8,fruit:"", name: "BANANA"),
        Fruit(identifier: 9,fruit:"", name: "CHERRY")
    ]
    
    var fruitIndex = [index]
    var fruitNumber = 0
    var score = 0
 

    mutating func checkAnswer(userAnswer: String) -> Bool {
        
        
        //Need to change answer to rightAnswer here.
        if userAnswer == fruitOptions[fruitNumber].fruit {
            score += 1 //We increase the value of score when we get the answer right.
            return true
        } else {
            return false
            
        }
    }
    
    func getScore() -> Int {
        return score
        
    }
    
    mutating func getFruitText() -> String {
    
        return fruitOptions[fruitNumber].name
         
        }
        
    
    
    func getProgress() -> Float {
        let progress = Float(fruitNumber) / Float(fruitOptions.count)
        return progress
        
        
    }
    
    mutating func nextFruit() {
        if fruitNumber + 1 < fruitOptions.count {
            
            fruitNumber += 1
            
        } else {
            
           // fruitNumber = 0
            score = 0
        }
    }
    
    mutating func shuffleFruitLabel(at index: Int) {
        fruitOptions.shuffle()
    }
    
    //TODO: Shuffle fruit on Buttons
    
    
    
}

控制器


import UIKit

class GameViewController: UIViewController {
    
    @IBOutlet weak var progressBar: UIProgressView!
    
    @IBOutlet private weak var fruitLabel: UILabel!
    
    @IBOutlet private weak var scoreLabel: UILabel!
    
    @IBOutlet private weak var playAgain: UIButton!
    
    @IBOutlet weak var optionButton0: UIButton!
    @IBOutlet weak var optionButton1: UIButton!
    @IBOutlet weak var optionButton2: UIButton!
    @IBOutlet weak var optionButton3: UIButton!
    @IBOutlet weak var optionButton4: UIButton!
    @IBOutlet weak var optionButton5: UIButton!
    @IBOutlet weak var optionButton6: UIButton!
    @IBOutlet weak var optionButton7: UIButton!
    @IBOutlet weak var optionButton8: UIButton!
    
    
    @IBOutlet private var fruitButtons: [UIButton]!
    
    var fruitness = Fruitness()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        updateUI()
        
    }
    @IBAction func replayButton(_ sender: UIButton) {
        //  fruitLabel.text = fruitness.fruitOptions[randomFruit].name
        
        //self.dismiss(animated: false, completion: nil)
        
    }
    @IBAction func touchButton(_ sender: UIButton)  {
        //--//
        let userAnswer = sender.currentTitle!
        let userGotItRight = fruitness.checkAnswer(userAnswer: userAnswer)
        
        if  userGotItRight {
            sender.backgroundColor = UIColor.green
        } else {
            sender.backgroundColor = UIColor.red
        }
        
        // fruitness.shuffleFruitTextWhenReset()
        fruitness.nextFruit()
        
        Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(updateUI), userInfo:nil, repeats: false)
    }
    
    @objc func updateUI() {
        
        
        optionButton0.backgroundColor = UIColor.clear
        optionButton1.backgroundColor = UIColor.clear
        optionButton2.backgroundColor = UIColor.clear
        optionButton3.backgroundColor = UIColor.clear
        optionButton4.backgroundColor = UIColor.clear
        optionButton5.backgroundColor = UIColor.clear
        optionButton6.backgroundColor = UIColor.clear
        optionButton7.backgroundColor = UIColor.clear
        optionButton8.backgroundColor = UIColor.clear
        
        fruitLabel.text = fruitness.getFruitText()
        progressBar.progress = fruitness.getProgress()
        scoreLabel.text = "SCORE: \(fruitness.getScore())"
        
    }
}

func imageOnButton(fruit: String, on button: UIButton) {
    if button.currentTitle == fruit {
        button.setTitle(fruit, for: UIControl.State.normal)
        
    }
}
import Foundation

struct Fruit {

    var identifier: Int
    var name: String
    var fruit: String
    
  
    init(identifier : Int, fruit : String, name : String) {
        self.identifier = identifier
        self.name = name
        self.fruit = fruit

    }

}

您可以根据新索引打乱 fruitOptions 数组和 re-assign 标识符。

例如:

fruitOptions.shuffle() // Shuffles the array

for (index, fruitOption) in fruitOptions.enumerated() {
      fruitOption.identifier = index + 1
}

添加 (index, fruitOption) 允许您遍历数组并访问每个项目的索引值,因此您可以将该索引值指定为新标识符(+1 因为我看到您开始标识符计数从 1 而不是 0)

.enumerated 只允许您执行我上面描述的操作。 您可以阅读更多相关内容 in Apple's documentations