简体   繁体   English

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

[英]“Thread 1: Fatal error, index out of range”

I tried to create an app in which random colours are being displayed in a random order.我试图创建一个应用程序,其中随机颜色以随机顺序显示。 The challenge should be to memorise their order and click the buttons underneath in the right order.挑战应该是记住他们的顺序并以正确的顺序单击下方的按钮。 Now when I tried to check the order with an array, the error "Thread 1: Fatal error, index out of range" appeared.现在,当我尝试使用数组检查顺序时,出现错误“线程 1:致命错误,索引超出范围”。 I'm new to coding and app development, but figured I have to start somewhere.我是编码和应用程序开发的新手,但我想我必须从某个地方开始。 I would really appreciate your help:)我将衷心感谢您的帮助:)

//
//  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()
    }
}

I think you never reach your我想你永远达不到你的

                    else if numberCache.count > 1 && numberCache.count <= 6{...

statement in your "tap me"-button to append to your numberCache before the app already tries to check your if numberCache[cache] == 0{ statement.在应用程序已经尝试检查您的if numberCache[cache] == 0{语句之前,您的“点击我”按钮中的 append 到您的 numberCache 中的语句。 Because of that there is only array 0 available.因此,只有数组 0 可用。

You can check this, if your app stopps crashing, when you change the code just for test purpose to if numberCache[0] == 0{ .如果您的应用程序停止崩溃,您可以检查这一点,当您出于测试目的将代码更改为if numberCache[0] == 0{

Despite of that use self.didTapstart.toggle() to change a Bool-value from true to false or vice versa instead of your complex if statement at the action of your tap-button.尽管如此,使用self.didTapstart.toggle()将 Bool 值从 true 更改为 false 或反之亦然,而不是在点击按钮操作时使用复杂的 if 语句。 ` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM