0
我想在Swift中编码A * Pathfinding。字典字符串等效问题
在这一点上,我在检索我的封闭列表的G-Costs时遇到了一个问题。
此问题是,当我尝试搜索词典中的条目时,即使我相信我正在输入正确的键,也会返回零。
这里是相关的代码(注意:字典是字符串:Int格式,因为它不需要CGPoints)。
print("\(closedList["\(currentPos)"])")
print("\(currentPosString)")
print("\(closedList)")
GCost = GCost + (Int)(closedList["\(currentPosString)"]!)
CurrentPosString是一个字符串,CurrentPos是一个CGPoint(我都试过)。
这里是从这段代码中读出的(我省略了很多代码)。
零
4,4
[ “4.0,4.0”:0]
(最后一行返回一个错误由于无)。
我的问题是我如何使'closedList [currentPosString]'具有正确的格式来成功访问条目并返回0?
代码生成closedPosString:
closedList["\(currentBest)"] = openList["\(currentBest)"]
openList["\(currentBest)"] = nil
openListOrder["\(currentBest)"] = nil
for i in 0 ..< mapTerrain.numberOfColumns{
for j in 0 ..< mapTerrain.numberOfRows{
if currentBest == "\(i), \(j)"{
currentPos = CGPoint(x: i, y: j)
currentPosString = "\(i), \(j)"
closedList["\(currentPos)"] = closedList[currentBest!]
print("\(closedList["\(currentPos)"])")
print("a") //not printing for some reason
foundNextNewPos = true
}
if foundNextNewPos == true{
break
}
}
if foundNextNewPos == true{
break
}
}
试图用这个代码重修,但仍打破了:currentBest的
currentPosString = currentBest!
currentBest = nil
代:
for key in openList.keys{
let tester = openList["\(key)"] //find way to not get a nil later
//print("\(openList["\(currentBest)"]!)") //gives nil
if key == "\(endPoint)"{
currentBest = key
foundEnd = true
break
}else if openList["\(currentBest)"] == nil{
currentBest = key
}else if tester! < openList["\(currentBest)"]!{
currentBest = key
} else if tester == openList["\(currentBest)"]{
if openListOrder["\(key)"]! > openListOrder["\(currentBest)"]!{
currentBest = key
}
}
}
你'currentPosString'的值为 “4,4”,而你的字典使用字符串键是 “4.0,4.0”。显然这两个字符串是不相同的。你如何生成“currentPosString”。 –
使用[NSStringFromCGPoint](https://developer.apple.com/reference/uikit/1624504-nsstringfromcgpoint)将点转换为字符串作为字典键使用 –
我现在没有我的代码,并且会得到回到你周一(因为我想我知道我做了什么,但我意识到可能有一个大问题,它不会选择正确的坐标,但不会导致此错误) –