1

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...

pikovayadama
  • 808
  • 2
  • 8
  • 26
  • exactly which function is causing this? Because you've got a number of array-key-generating areas in there. – Marc B Jun 18 '15 at 20:31
  • " Whenever an outside class callsConstants.drawingTypes or Constants.colorFlags, I get the following error:" Ok, there's nothing wrong with the code here. The "outside class" is using an index to the array that is wrong. http://stackoverflow.com/a/24132618/290072 This answer shows that your random statement should be correct. – Almo Jun 18 '15 at 21:01
  • The outside class actually doesn't actually ever access an index - instead it uses the functions defined here - randomColorFlag() etc. – pikovayadama Jun 18 '15 at 21:12
  • Looking at your debugger window, I'd be more interested in how `min = 883820` when you clearly set it to 1 in the function. The rest are just artifacts of converting between `UInt` and `Int` when values are out of bound. Can you place a conditional break point on `let min = 1` to find why it become negative? – Code Different Jun 19 '15 at 16:28
  • Yeah, I can't explain anything that's happening there. But after I cleaned the build, restarted the computer, restarted Xcode, and re-built everything, the problem went away. SO..... go figure. – pikovayadama Jun 19 '15 at 21:15

0 Answers0