gpt4 book ai didi

swift - 使用类作为键的字典数组中的索引超出范围 - swift

转载 作者:行者123 更新时间:2023-11-30 13:36:08 26 4
gpt4 key购买 nike

为什么这不起作用?

我尝试将字典的值分配给 false 的错误是它失败的地方。它返回此错误“ fatal error :数组索引超出范围”,如底部的输出所示。

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++
}
}

这就是我的字典数组的设置方式:

var arrayOfTupleClass = [TupleClass]()
var pointDictionaryArray = [[TupleClass: Bool]]()

这是我的 TupleClass,它应该涵盖将类作为字典的键,因为我使其可散列。

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

这是输出:

arrayOfTupleClass.count: 1
arrayOfTupleClass[tupleCount]: My_Project_Name.TupleClass
tupleCount: 0
imageNum: 0
fatal error: Array index out of range

最佳答案

据我所知,您不能使用 swift 类名作为引用(也不能使用较新版本中的 Obj-C 类名)。但是,为什么现在引用

    let myclassName = self.className
let anotherClassName = NSClassFromString(self.classForCoder)

我刚刚拼凑了一个快速的 Playground 示例来玩:

import Foundation

debugPrint("Hello, World, Here we go from the PLayground template!")

let width = 100 // from your code
let height = 100 // from yoru code

typealias theTup = (newX: CGFloat, newY: CGFloat, newXMax: CGFloat, newYMax: CGFloat) // setting a typ alias to your tuple format


var theTupArray: [theTup] /*Strong typing*/ = [theTup]() // lets make a new array (with default zero element value)

// lets add some default values for testing, nothing special
theTupArray.appendContentsOf([
(newX: 1.0, newY: 2.0, newXMax: 2.0, newYMax: 5.0),
(newX: 2.0, newY: 3.0, newXMax: 4.0, newYMax: 10.0),
(newX: 3.0, newY: 4.0, newXMax: 6.0, newYMax: 15.0),
]
)

// now for-each element in the `theTupArray`... See Swift .forEach.. Or other shorthand methods like .map etc...

theTupArray.forEach { (
newX: CGFloat,
newY: CGFloat,
newXMax: CGFloat,
newYMax: CGFloat) in

debugPrint("NewX 1:\(newX), NewY: \(newY), NewXMax: \(newXMax), NewYMax: \(newYMax)", terminator: "\n")

}

希望这个快速重写有帮助:)

关于swift - 使用类作为键的字典数组中的索引超出范围 - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36051619/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com