我有以下声明的Constants类。每当外部课程调用Constants.drawingTypes
或Constants.colorFlags
时,我都会收到以下错误:
致命错误:数组索引超出范围
我担心我可能会误解static
关键字,但是这个方法和成员的意图是安排一组常量和我的应用程序中使用的数组。 。
我在这里,在声明或其他方面是否遗漏了一些明显的错误?
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]
}
}
编辑:
经过多次调试之后,我注意到第65行发生了以下情况(有时只发生!)
return Constants.allDrawingTypes[i]
当方法找到索引项时,返回时,max, min and i
的值以某种方式重置,这是错误发生的方式。 (您可以在调试区域中查看它们的值)。很明显,Constants.allDrawingTypes[-1074625864]
会抛出异常。问题是,为什么在获取静态数组后重置值?这就是我认为我不能正确理解静力学的地方......