Why isn't this working?
The error where I'm trying to assign the value of the dictionary to false is where it's failing. It returns this error "fatal error: Array index out of range" as shown in the output at the bottom.
var tupleCount = 0
for var i = 0; i < width; ++i {
for var j = 0; j < height; ++j {
arrayOfTupleClass.append(TupleClass(newX: i, newY: j, newXMax: width, newYMax: height))
print("arrayOfTupleClass.count: \(arrayOfTupleClass.count)")
print("arrayOfTupleClass[tupleCount]: \(arrayOfTupleClass[tupleCount])")
print("tupleCount: \(tupleCount)")
print("imageNum: \(imageNum)")
// placing '0' in place of dictionary Array index for simplicity
pointDictionaryArray[0][arrayOfTupleClass[tupleCount]] = false // <-- error here
tupleCount++
}
}
This is how my array of dictionaries is set up:
var arrayOfTupleClass = [TupleClass]()
var pointDictionaryArray = [[TupleClass: Bool]]()
this is my TupleClass which should cover for having a class as a key for a dictionary, because I made it hashable.
class TupleClass: Hashable {
var x: Int!
var y: Int!
let yMax: Int!
let xMax: Int!
var weight: Int = 0
init(newX: Int, newY: Int, newXMax: Int, newYMax: Int) {
x = newX
y = newY
yMax = newYMax
xMax = newXMax
}
func setWeight(newWeight: Int) {
weight = newWeight
}
func getWeight() -> Int {
return weight
}
// required for the Hashable protocol
var hashValue: Int {
return x * yMax + y
}
};
// required function for the Equatable protocol, which Hashable inheirits from
func ==(left: TupleClass, right: TupleClass) -> Bool {
return (left.x == right.x) && (left.y == right.y)
}
This is the output:
arrayOfTupleClass.count: 1
arrayOfTupleClass[tupleCount]: My_Project_Name.TupleClass
tupleCount: 0
imageNum: 0
fatal error: Array index out of range