如何从字典中删除某个CGPoint [英] How can I remove a certain CGPoint from a dictionaries
问题描述
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。点]]
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.p oint,pt4.point],
3:[pt16.point,pt15.point,pt14.point]]
allPoints = [pt1,pt2,pt3,pt4,pt5]
如何迭代去除 但是我得到错误:$ b (key:Int,value:[CGPoint])'(aka'(key:Int,value:Array< CGPoint>)')的作用是 在行: 编辑 UPDATED QUESTION 基于Rob Napier的建议,我更新了这个问题: : 其中 我有以下地点:$ b 从 func locationsRemoving(changedLoc removePoint:CGPoint,from locations:[Location]) - > location.changedLoc {$ b $ return location.map {location in I have seen a number of posts on looping through a dictionary but they are a different and simpler than what I want to achieve here, in my opinion. I have the following arrays: How can I iterate to remove I tried the following: but I got the error: at the line: EDIT The struct that creates each of the points is below: UPDATED QUESTION Based on Rob Napier’s suggestion I’ve updated the question: I have a struct below: where I have the following Locations From
这篇关于如何从字典中删除某个CGPoint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! pt3.point
是来自所有 Int
- [CGPoint]
字典的
在pt.array中为pt {
为pt.arrays中的ptArrIndex {
$ b $为ptArrIndex中的(key,value){
//从此处的字典中删除点
}
$ / code $ / pre
$ b
不符合协议'序列'
$ b $ p $ for(key,value)in ptArrIndex {
创建每个点的结构如下所示:
$ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ var点:CGPoint
var arrays:[Int:[CGPoint]]
}
struct位置{
var point:CGPoint
var changedLoc:[Int:[CGPoint]]
点
表示 Location 和
CGPoints changedLoc
表示所有可能的组 Location
可以变成。
$ b
var (点: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:[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)
它位于 location1
和 location2
changedLoc
解析方法
var changedLoc:[Int:[CGPoint]] = [:]
for(key,values) $ b $ changedLoc [key] = values.filter {$ 0!= removePoint}
}
return位置(point:location.point,changedLoc:changedLoc)
}
}
$ b $ let removePoint = CGPoint(x:421.0,y:43.0)
print(allLocations)
print(locationsRemoving(changedLoc:removePoint,from:allLocations))
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]
pt3.point
which is a CGPoint from all the Int
-[CGPoint]
dictionaries in the allPoints
array?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]]
}
struct Location {
var point: CGPoint
var changedLoc: [Int: [CGPoint]]
}
point
represents a CGPoint
for Location
and changedLoc
represents all the possible groups of CGPoints Location
can change to. I calculate this randomly.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
how can I remove the point CGPoint(x: 421.0, y: 43.0)
which is in both location1
and location2
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))
登录
关闭