问题
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:
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]
How can I iterate to remove pt3.point which is a CGPoint from all the Int-[CGPoint] dictionaries in the allPoints array?
I tried the following:
for pt in allPoints {
for ptArrIndex in pt.arrays {
for (key, value) in ptArrIndex {
//remove point from dict here
}
}
}
but I got the error:
Type '(key: Int, value:[CGPoint])' (aka'(key: Int, value: Array<CGPoint>)') does not conform to protocol 'Sequence'
at the line:
for (key, value) in ptArrIndex {
EDIT
The struct that creates each of the points is below:
struct Point {
var point: CGPoint
var arrays: [Int: [CGPoint]]
}
UPDATED QUESTION
Based on Rob Napier’s suggestion I’ve updated the question:
I have a struct below:
struct Location {
var point: CGPoint
var changedLoc: [Int: [CGPoint]]
}
where point represents a CGPoint for Location and changedLoc represents all the possible groups of CGPoints Location can change to. I calculate this randomly.
I have the following Locations
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]
From allLocations how can I remove the point CGPoint(x: 421.0, y: 43.0) which is in both location1 and location2 changedLoc?
回答1:
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))
回答2:
The confusing thing you have is that the property named array in the ptN instances is actually a Dictionary<Int, [CGPoint]>
So, for the structure you currently have, you'll need to amend your code to match:
for pt in allPoints {
for (key, ptArray) in pt.array {
for elem in ptArray {
//remove point from dict here
}
}
}
来源:https://stackoverflow.com/questions/42120076/how-can-i-remove-a-certain-cgpoint-from-a-dictionaries