“线程1:致命错误,索引超出范围”

时间:2020-05-14 08:23:58

标签: ios swiftui

我试图创建一个应用,其中以随机顺序显示随机颜色。挑战应该是记住他们的顺序,然后按正确的顺序单击下面的按钮。现在,当我尝试使用数组检查顺序时,出现错误“线程1:致命错误,索引超出范围”。我是编码和应用程​​序开发的新手,但我想必须从某个地方开始。非常感谢您的帮助:)

//
//  ContentView.swift
//  randomColorAufKnopfdruck
//
//  Created by Jeff on 13.05.20.
//  Copyright © 2020 Jeff. All rights reserved.
//

import SwiftUI

class TestColor{
    var chosenColor = Color.black
    var colors = [Color.green, .blue, .red, .pink, .orange, .purple]
    var id: Int
    var numbers = [0,1,2,3,4,5]

    init(){
        id = numbers.randomElement()!
    }


    func generateRDMColor() -> Color{
        if id<=5{
            switch id{
            case 0:
                chosenColor = colors[id]
            case 1:
                chosenColor = colors[id]
            case 2:
                chosenColor = colors[id]
            case 4:
                chosenColor = colors[id]
            case 5:
                chosenColor = colors[id]
            default:
                break;
            }
        }
        return chosenColor
    }
}

class ColorButtons{
    var colorRepresenting: Color
    enum Colors
    {
        case red
        case blue
        case green
        case purple
        case orange
        case pink
    }

    init(color: Colors){
        switch color{
        case .red:
            colorRepresenting = Color.red
        case .blue:
            colorRepresenting = Color.blue
        case .green:
            colorRepresenting = Color.green
        case .purple:
            colorRepresenting = Color.purple
        case .orange:
            colorRepresenting = Color.orange
        case .pink:
            colorRepresenting = Color.pink
        }

    }

}

public var outputColor = Color.black
public var numberCache = [0]
public var cache = 0
public var score = 0

struct ContentView: View {
    @State private var didTapstart:Bool = false
    var body: some View {
        VStack{
            Spacer()
            RoundedRectangle(cornerRadius: 3)
            .foregroundColor(self.didTapstart ? outputColor : outputColor)
            Spacer()
            HStack{
                Spacer()
                Button(action: {
                    if self.didTapstart == false{
                        self.didTapstart = true
                    }
                    else{
                        self.didTapstart = false
                    }
                    outputColor = TestColor.init().generateRDMColor()
                    if numberCache.count <= 1{

                        switch outputColor{
                        case .green:
                            numberCache[0] = 0
                        case .blue:
                            numberCache[0] = 1
                        case .red:
                            numberCache[0] = 2
                        case .pink:
                            numberCache[0] = 3
                        case .orange:
                            numberCache[0] = 4
                        case .purple:
                            numberCache[0] = 5
                        default:
                            numberCache[0] = 0
                        }
                    }
                    else if numberCache.count > 1 && numberCache.count <= 6{

                        switch outputColor{
                        case .green:
                            numberCache.append(0)
                        case .blue:
                            numberCache.append(1)
                        case .red:
                            numberCache.append(2)
                        case .pink:
                            numberCache.append(3)
                        case .orange:
                            numberCache.append(4)
                        case .purple:
                            numberCache.append(5)
                        default:
                            numberCache.removeAll()
                            numberCache[0] = 0
                        }
                    }
                    else{
                        numberCache.removeAll()
                        outputColor = Color.gray

                    }
                }) {
                    Text("press me")
                }
                Spacer()
            }
            Spacer()
            Text(String(score))
            HStack{
                Spacer()
                Button(action: {
                    if cache <= 6{
                        cache += 1
                    }
                    else{
                        cache = 0
                    }
                    if numberCache[cache] == 0{
                        score += 1
                    }
                    else{
                        score = 0
                    }
                }) {
                    RoundedRectangle(cornerRadius: 3)
                        .foregroundColor(ColorButtons.init(color: .green).colorRepresenting)
                }
                Spacer()
                Button(action: {
                    if cache <= 5{
                        cache += 1
                    }
                    else{
                        cache = 0
                    }
                    if numberCache[cache] == 1{
                        score += 1
                    }
                    else{
                        score = 0
                    }
                }) {
                    RoundedRectangle(cornerRadius: 3)
                        .foregroundColor(ColorButtons.init(color: .blue).colorRepresenting)
                }
                Spacer()
                Button(action: {
                    if cache <= 5{
                        cache += 1
                    }
                    else{
                        cache = 0
                    }
                    if numberCache[cache] == 2{
                        score += 1
                    }
                    else{
                        score = 0
                    }
                }) {
                    RoundedRectangle(cornerRadius: 3)
                        .foregroundColor(ColorButtons.init(color: .red).colorRepresenting)
                }
                Spacer()
            }
            HStack{
                Spacer()
                Button(action: {
                    if cache <= 5{
                        cache += 1
                    }
                    else{
                        cache = 0
                    }
                    if numberCache[cache] == 3{
                        score += 1
                    }
                    else{
                        score = 0
                    }
                }) {
                    RoundedRectangle(cornerRadius: 3)
                        .foregroundColor(ColorButtons.init(color: .pink).colorRepresenting)
                }
                Spacer()
                Button(action: {
                    if cache <= 5{
                        cache += 1
                    }
                    else{
                        cache = 0
                    }
                    if numberCache[cache] == 4{
                        score += 1
                    }
                    else{
                        score = 0
                    }
                }) {
                    RoundedRectangle(cornerRadius: 3)
                        .foregroundColor(ColorButtons.init(color: .orange).colorRepresenting)
                }
                Spacer()
                Button(action: {
                    if cache <= 5{
                        cache += 1
                    }
                    else{
                        cache = 0
                    }
                    if numberCache[cache] == 5{
                        score += 1
                    }
                    else{
                        score = 0
                    }
                }) {
                    RoundedRectangle(cornerRadius: 3)
                        .foregroundColor(ColorButtons.init(color: .purple).colorRepresenting)
                }
                Spacer()
            }
            Spacer()
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您永远都无法达到

                    else if numberCache.count > 1 && numberCache.count <= 6{...
在应用程序已经尝试检查您的if numberCache[cache] == 0{之前,在“点击我”按钮中的

语句将其追加到numberCache 声明。因此,只有数组0可用。

当您将代码(仅用于测试目的)更改为if numberCache[0] == 0{时,如果您的应用程序崩溃了,您可以进行检查。

尽管如此,您仍可以使用self.didTapstart.toggle()将布尔值从true更改为false,反之亦然,而不是在轻按按钮操作时使用复杂的if语句。 `