У меня есть разные массивы, они хранят разное количество элементов, при нажатии кнопки индекс массива увеличивается на 1, и элемент добавляется в другой массив, я сделал проверку диапазона массива, но это делает не работа. в чем может быть проблема
Консоль xCode: фатальная ошибка: индекс вне диапазона: файл
class ViewController: UIViewController {
var currentIndex = 0
var arrayIndex = -1
var arrayWords: [Words]? {
didSet {
// wordTextLabel.text = arrayWords?[0].arrayWords?[currentIndex].name
// wordImage.image = arrayWords?[0].arrayWords?[currentIndex].image
// wordArray.append((arrayWords?[0].arrayWords?[currentIndex])!)
}
}
var testArray = ["Hello","Energy","Disk","Duck","Wafles"]
var wordArray: [String] = [] //[ArrayWords] = []
lazy var wordTextLabel: UILabel = {
let label = UILabel()
label.text = testArray[currentIndex]//arrayWords?[0].arrayWords?[currentIndex].name
label.textColor = .white
label.font = UIFont(name: "OpenSans-ExtraBold", size: 20)
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
label.isUserInteractionEnabled = true
return label
}()
lazy var wordImage: UIImageView = {
let image = UIImageView()
image.contentMode = .scaleAspectFit
image.tintColor = .white
image.translatesAutoresizingMaskIntoConstraints = false
return image
}()
lazy var button1: UIButton = {
let button = UIButton()
button.backgroundColor = .white
button.setTitle("Good", for: .normal)
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
button.layer.cornerRadius = 40
button.clipsToBounds = true
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
lazy var button2: UIButton = {
let button = UIButton()
button.backgroundColor = .white
button.setTitle("No", for: .normal)
button.setTitleColor(.black, for: .normal)
button.layer.cornerRadius = 40
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
func setupView() {
view.setGradient(colorOne: Color.purpleColor, colorTwo: Color.pinkColor,
startPoint: CGPoint(x: 1.0, y: 0.1), endPoint: CGPoint(x: 0.1, y: 1.0))
view.addSubview(wordTextLabel)
//wordTextLabel.addSubview(wordImage)
//view.addSubview(wordImage)
view.addSubview(button1)
view.addSubview(button2)
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|-120-[v0]-120-|", metrics: nil, views: ["v0": wordTextLabel]))
button1.bottomAnchor.constraint(lessThanOrEqualTo: view.bottomAnchor, constant: -40).isActive = true
button1.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 30).isActive = true
button1.heightAnchor.constraint(equalToConstant: 80).isActive = true
button1.widthAnchor.constraint(equalToConstant: 80).isActive = true
button2.bottomAnchor.constraint(lessThanOrEqualTo: view.bottomAnchor, constant: -40).isActive = true
button2.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -30).isActive = true
button2.heightAnchor.constraint(equalToConstant: 80).isActive = true
button2.widthAnchor.constraint(equalToConstant: 80).isActive = true
// wordArray.append((arrayWords?[0].arrayWords?[currentIndex])!)
}
@objc func buttonAction(sender: Any!) {
print("Button tapped")
action()
}
func action() {
if currentIndex < testArray.count/*arrayWords?[0].arrayWords?.count*/ { //!= 2 // !=
currentIndex += 1
arrayIndex += 1
wordTextLabel.text = testArray[currentIndex]//arrayWords?[0].arrayWords?[currentIndex].name
wordArray.append(testArray[arrayIndex])//((arrayWords?[0].arrayWords?[arrayIndex])!)
} else if arrayIndex != 3 {
arrayIndex += 1
wordArray.append(testArray[arrayIndex])//((arrayWords?[0].arrayWords?[arrayIndex])!)
}
}
}