gpt4 book ai didi

swift - 如何从字典中删除某个 CGPoint

转载 作者:行者123 更新时间:2023-11-28 12:30:54 24 4
gpt4 key购买 nike

我看过很多关于遍历字典的帖子,但在我看来,它们与我在这里想要实现的目标不同且更简单。我有以下数组:

pt1.array = [0:[pt2.point], 
1:[pt8.point, pt12.point, pt4.point],
2:[pt20.point, pt14.point, pt3.point],
3:[pt7.point, pt8.point, pt9.point]]
pt2.array = [0:[pt5.point],
1:[pt8.point, pt11.point, pt1.point],
2:[pt10.point, pt9.point, pt3.point],
3:[pt6.point, pt1.point, pt4.point]]
pt3.array = [0:[pt13.point],
1:[pt1.point, pt15.point, pt7.point],
2:[pt19.point, pt14.point, pt2.point],
3:[pt10.point, pt11.point, pt12.point]]
pt4.array = [0:[pt8.point],
1:[pt9.point, pt11.point, pt13.point],
2:[pt14.point, pt15.point, pt6.point],
3:[pt3.point, pt2.point, pt1.point]]
pt5.array = [0:[pt18.point],
1:[pt8.point, pt6.point, pt1.point],
2:[pt3.point, pt17.point, pt4.point],
3:[pt16.point, pt15.point, pt14.point]]

allPoints = [pt1, pt2, pt3, pt4, pt5]

我如何迭代以从 中的所有 Int-[CGPoint] 字典中删除 pt3.point 这是一个 CGPoint >allPoints 数组?

我尝试了以下方法:

for pt in allPoints {
for ptArrIndex in pt.arrays {

for (key, value) in ptArrIndex {
//remove point from dict here
}
}
}

但是我得到了错误:

Type '(key: Int, value:[CGPoint])' (aka'(key: Int, value: Array<CGPoint>)') does not conform to protocol 'Sequence'

在线:

for (key, value) in ptArrIndex   {

编辑

创建每个点的结构如下:

struct Point {
var point: CGPoint
var arrays: [Int: [CGPoint]]
}

更新问题

根据 Rob Napier 的建议,我更新了问题:

我有一个结构如下:

struct Location {
var point: CGPoint
var changedLoc: [Int: [CGPoint]]
}

其中 point 表示 LocationCGPointchangedLoc 表示所有可能的 CGPoints 组Location 可以更改为。我是随机计算的。

我有以下位置

var location1 = Location(point: initialBallPosition, changedLoc: [0: [CGPoint(x: 421.0, y: 43.0), CGPoint(x: 202.0, y: 69.0)], 1: [CGPoint(x: 121.0, y: 198.0)]])
var location2 = Location(point: initialBallPosition, changedLoc: [0: [CGPoint(x: 421.0, y: 43.0), CGPoint(x: 123.0, y: 254.0)], 1: [CGPoint(x: 90.0, y: 104.0)]])

var allLocations = [location1, location2]

allLocations 我怎样才能删除点 CGPoint(x: 421.0, y: 43.0) 这在 location1location2 changedLoc?

最佳答案

func locationsRemoving(changedLoc removePoint: CGPoint, from locations: [Location]) -> [Location] {
return locations.map { location in
var changedLoc: [Int: [CGPoint]] = [:]
for (key, values) in location.changedLoc {
changedLoc[key] = values.filter{ $0 != removePoint }
}
return Location(point: location.point, changedLoc: changedLoc)
}
}

let removePoint = CGPoint(x: 421.0, y: 43.0)
print(allLocations)
print(locationsRemoving(changedLoc: removePoint, from: allLocations))

关于swift - 如何从字典中删除某个 CGPoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42120076/

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