Why am I getting 'fatal error: Array index out of range' with this code?

306 views Asked by At

I have the following Constants class declared as such. Whenever an outside class callsConstants.drawingTypes or Constants.colorFlags, I get the following error:

fatal error: Array index out of range

I'm afraid I might be misunderstanding the static keyword, but the intent of the methods and members of this class was to arrange for a set of constants and arrays of those to be used in my application.

Is there some obvious error that I'm missing here, in declarations or otherwise?

import Foundation

class Constants {

     enum drawingTypes: String, Printable {
        case Any = "Any"
        case Line = "Line"
        case CubicBezier = "Cubic Bezier"
        case SquareBezier = "Square Bezier"
        case LineFilled = "Line Filled"
        case CubicFilled = "Cubic Filled"
        case SquareBezierFilled = "Square Bezier Filled"
        case MultiLayer = "MultiLayer"

        var description: String { return rawValue }
    }

    enum colorFlags : String, Printable
    {
        case darks = "Darks"
        case pastels = "Pastels"
        case fullRange = "Full Range"
        case greys = "Greys"

        var description : String {return rawValue }
    }

    static let allColorFlags = [colorFlags.darks,
                                colorFlags.pastels,
                                colorFlags.fullRange,
                                colorFlags.greys]

    static let allDrawingTypes = [drawingTypes.Any,
                                 drawingTypes.Line,
                                drawingTypes.CubicBezier,
                                drawingTypes.SquareBezier,
                                drawingTypes.LineFilled,
                                drawingTypes.CubicFilled,
                                drawingTypes.SquareBezierFilled,
                                drawingTypes.MultiLayer]


    class func randomTypeIncludingMulti() -> drawingTypes {
        return randomType(true)
    }

    class func randomType(includingMulti : Bool) -> drawingTypes {
        let min = 1
        var max = Constants.allDrawingTypes.count

        if includingMulti == false
            { max = max - 1 }

        let i = Int(arc4random_uniform(UInt32(max - min))) + min
        return Constants.allDrawingTypes[i]
    }

    class func randomColorFlag() -> colorFlags {
        let min = 0
        let max = Constants.allColorFlags.count
        let i = Int(arc4random_uniform(UInt32(max - min))) + min
        return Constants.allColorFlags[i]
    }

}

EDIT :

enter image description here

After debugging this some more, I noticed that the following happens on line 65 (only sometimes!)

return Constants.allDrawingTypes[i]

When the method goes to find the indexed item, upon return, the values for max, min and i reset somehow, which is how the error occurs. (You can see their values in the debug area). Clearly, Constants.allDrawingTypes[-1074625864] is going to throw an exception. The question is, why are the values being reset after going to get the static array? This is where I'm thinking I am not understanding statics correctly...

0

There are 0 answers