Swift 字典字符串等价问题
我正在尝试用Swift编写一个*寻路代码 在这一点上,我遇到了一个问题,检索我的封闭列表的G成本 这个问题是,当我试图在字典中搜索条目时,即使我认为输入的是正确的键,也会返回nil 以下是相关代码(注意:Dictionary是string:Int格式的,因为它不需要CGPoints) CurrentPosString是一个字符串,CurrentPos是一个CGPoint(我两个都试过了) 下面是从这段代码中读出的内容(我省略了很多代码) 零 4,4 [“4.0,4.0”:0] (最后一行返回一个由nil引起的错误) 我的问题是如何使“closedList[CurrentPossString]”具有正确的格式以成功访问条目并返回0 生成ClosedPostString的代码:Swift 字典字符串等价问题,swift,dictionary,null,path-finding,Swift,Dictionary,Null,Path Finding,我正在尝试用Swift编写一个*寻路代码 在这一点上,我遇到了一个问题,检索我的封闭列表的G成本 这个问题是,当我试图在字典中搜索条目时,即使我认为输入的是正确的键,也会返回nil 以下是相关代码(注意:Dictionary是string:Int格式的,因为它不需要CGPoints) CurrentPosString是一个字符串,CurrentPos是一个CGPoint(我两个都试过了) 下面是从这段代码中读出的内容(我省略了很多代码) 零 4,4 [“4.0,4.0”:0] (最后一行返回一个
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的生成:
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
}
}
}
openList.keys中的键的{
让tester=openList[“\(key)”]//找到以后不获取nil的方法
//打印(“\(openList[“\(currentBest)”]!”/)将给出零
如果键=“\(端点)”{
当前最佳值=键
foundEnd=true
打破
}如果openList[“\(currentBest)”]==nil,则为else{
当前最佳值=键
}否则,如果测试仪!openListOrder[“\(当前最佳)”{
当前最佳值=键
}
}
}
问题在于没有条目“4,4”,条目“4.0,4.0”来自于依赖CGFloats生成密钥的早期代码行,编辑此条目以使用相同的格式(“Int,Int”)使其工作
if hexCounter < 3 && openList["\(currentPos.x), \(currentPos.y)"] == nil{
closedList["\(currentPos.x), \(currentPos.y)"] = 0
}
如果hexCounter<3&&openList[“\(currentPos.x),\(currentPos.y)”]==nil{
closedList[“\(当前位置x),\(当前位置y)”]=0
}
到
如果hexCounter<3&&openList[“\(currentPosString)”]==nil{
closedList[“\(CurrentPoString)”]=0
}
您的CurrentPoString
具有值“4,4”,而您的字典使用的字符串键为“4.0,4.0”。显然,这两个字符串并不相等。如何生成“currentPossString”。用于将点转换为字符串以用作字典键我现在没有代码,周一会给你回复(我想我知道我做了什么,但我意识到如果没有选择正确的坐标,可能会有很大的问题,但不会导致此错误)我的想法是,我使用两个循环在字典中进行迭代,以找到具有最小F成本的openList磁贴,然后通过该循环返回其坐标(因为我不知道如何直接将字符串转换为CGPoint),然后通过简单的转换将其转换为CGPoint”(I),(j)”(这是我在其他方面使用的格式)。我将在周一再次访问代码时粘贴该代码。添加了代码。
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
}
}
}
if hexCounter < 3 && openList["\(currentPos.x), \(currentPos.y)"] == nil{
closedList["\(currentPos.x), \(currentPos.y)"] = 0
}
if hexCounter < 3 && openList["\(currentPosString)"] == nil{
closedList["\(currentPosString)"] = 0
}