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

标签 swift sorting dictionary filter swift3

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

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/

相关文章:

从字典中查找加权最小值和最大值键的 Pythonic 方法

ios - 将 View 顶部对齐到垂直堆栈 View 中另一个 View 的底部

php - 使用 DISTINCT 和 ORDER BY 进行 MySQL 查询

ios - "unable to dequeue a cell with identifier"当切换到编程转换而不是 segue 时

sql - NHibernate 排序(SQL 作为第二个选项)

python - 类型错误 : unorderable types: tuple() < int() in Python 3

Python请求获取JSON

python - 为什么字典键被转换为继承的类类型?

ios - animateWithTextures 错误,XCode 6 beta,.swift SpriteKit

ios - 有人可以清楚地解释 FIRDataEventType 的 .Value、.ChildAdded、.ChildChanged、.ChildRemoved 之间的区别吗?