swift 5 Thread 1: Fatal error: Array index is out of range

for i in dictionaryCollections[countOfWordsForDictionaryCheck2*2..<countOfWordsForDictionaryCheck*2+ind*2] {
            if check {
                dictionary1Key.append(i)
                dictionaryCollections.remove(at: 0)
                check = false
            } else {
                dictionary1Value.append(i)
                dictionaryCollections.remove(at: 0)
                check = true
            }
        }

Проблема появляется в создании for Весь код:

import UIKit

class ViewController: UIViewController {
    
    func WordsCardValue(_ sender: UIButton) {
        buttonWords.isHidden = true
        buttonWords.setTitle(dictionary1Value[0], for: .normal)
        dictionary1Value.remove(at: 0)
        buttonWords.isHidden = false
        if dictionary1Key.isEmpty {
            buttonsMassive.remove(at: senderCheckIndex)
            buttonWords.isHidden = true
            for i in 0..<buttonsMassive.count {
                buttonsMassive[i].isHidden = false
            }
        } else {
            WordsCard(sender)
        }
        

    }
    
    @objc func WordsCard(_ sender: UIButton) {
        countOfWordsForDictionaryCheck = 0
        countOfWordsForDictionaryCheck2 = 0
        ind = 0
        check = true
        for i in 0..<buttonsMassive.count {
            buttonsMassive[i].isHidden = true
        }
        ind = 0
        senderCheckIndex = Int(buttonsMassive.firstIndex(of: sender)!)
        
        dictionary1Key.removeAll()
        dictionary1Value.removeAll()
        print(countOfWordsForDictionary[ind])
        while ind != senderCheckIndex {
            countOfWordsForDictionaryCheck += Int(countOfWordsForDictionary[ind])
            ind += 1
        }
        print(countOfWordsForDictionaryCheck2, countOfWordsForDictionaryCheck)
        countOfWordsForDictionaryCheck2 = countOfWordsForDictionaryCheck
        print(countOfWordsForDictionaryCheck2, countOfWordsForDictionaryCheck)
        if countOfWordsForDictionaryCheck*2+ind*2 == 0 {
            countOfWordsForDictionaryCheck = countOfWordsForDictionary[0]
        }
        print(countOfWordsForDictionaryCheck2, countOfWordsForDictionaryCheck, ind)
        for i in dictionaryCollections[countOfWordsForDictionaryCheck2*2..<countOfWordsForDictionaryCheck*2+ind*2] {
            if check {
                dictionary1Key.append(i)
                dictionaryCollections.remove(at: 0)
                check = false
            } else {
                dictionary1Value.append(i)
                dictionaryCollections.remove(at: 0)
                check = true
            }
        }
        
        namesCollectionForCardsLabel.text = sender.titleLabel?.text
        
        buttonWords.setTitle(dictionary1Key[0], for: .normal)
        dictionary1Key.remove(at: 0)
        buttonWords.isHidden = false
        WordsCardValue(sender)
    }
    
    var namesForButtonCollection : [String] = ["fruits", "city", "clock"]
    
    var countOfWordsForDictionary : [Int] = [4, 3, 2]
    
    var countDelete = 0
    
    var check = true
    var buttonNamesCheck = 0
    var senderCheckIndex = 0
    var senderCheckIndex2 = 0
    var ind = 0
    var countOfWordsForDictionaryCheck = 0
    var countOfWordsForDictionaryCheck2 = 0
    
    var buttonWords = UIButton()
    
    var a = 1
    
    let buttonsColumnSize : Int = Int(round(UIScreen.main.bounds.width / 2 - 30))
    let buttonsColumnPlace2 : Int = Int(round(UIScreen.main.bounds.width / 2 + 10))
    var buttonWordsSize : Int = Int(round(UIScreen.main.bounds.width)) - Int(round(UIScreen.main.bounds.width/10))
    
    var buttonsMassive = [UIButton]()
    
    var namesCollectionForCardsLabel = UILabel()

    var buttonCount = true
    
    var dictionaryCollections : [String] = ["Apple", "Яблоко", "Grape", "Виноград", "Garnet", "Гранат", "Pear", "Груша", "Market", "Рынок", "House", "Дом", "Flat", "Квартира", "Ten", "Десять", "Clock", "Часы"]
    
    var dictionary1Key : [String] = []
    var dictionary1Value : [String] = []
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        buttonWords.frame.size = CGSize(width: buttonWordsSize, height: buttonWordsSize)
        buttonWords.layer.cornerRadius = CGFloat(roundf(Float(buttonsColumnSize / 10)))
        buttonWords.center = self.view.center
        buttonWords.backgroundColor = .placeholderText
        buttonWords.setTitleColor(.darkText, for: .normal)
        self.view.addSubview(buttonWords)
        buttonWords.isHidden = true
        
        
        
        namesCollectionForCardsLabel.font = UIFont(name: "systemFont", size: round(UIScreen.main.bounds.width / 20))
        namesCollectionForCardsLabel.backgroundColor = .clear
        namesCollectionForCardsLabel.frame = CGRect(x: 0, y: 30, width: 100, height: 100)
        namesCollectionForCardsLabel.center.x = self.view.center.x
        
        createButtons()
        view.addSubview(namesCollectionForCardsLabel)
    }
    
    private func createButtons() {
        
        
        
        var y = 50
        
        
        for i in 0..<namesForButtonCollection.count {

            var buttonCollectionFrame = CGRect(x: 20, y: y, width: buttonsColumnSize, height: buttonsColumnSize)
            var buttonCollectionFrame2 = CGRect(x: buttonsColumnPlace2, y: y, width: buttonsColumnSize, height: buttonsColumnSize)
            let button = UIButton(type: .system)
            if buttonCount {
                button.frame = buttonCollectionFrame
                buttonCount = false
            } else {
                button.frame = buttonCollectionFrame2
                buttonCount = true
                y += buttonsColumnSize + 20
            }
            button.backgroundColor = .gray
            button.titleLabel?.textAlignment = .center
            
            button.layer.cornerRadius = CGFloat(roundf(Float(buttonsColumnSize / 10)))
            
            
            
            
            view.addSubview(button)
            buttonsMassive.append(button)
            button.addTarget(self, action: #selector(WordsCard(_ :)), for: .touchUpInside)
            }
        for i in buttonsMassive[0..<namesForButtonCollection.count] {
            i.setTitle(namesForButtonCollection[buttonNamesCheck], for: .normal)
            buttonNamesCheck += 1
        }
        buttonNamesCheck = 0
        
        }
        
    }

Ответы (0 шт):