如何从字典

时间:2017-02-08 17:42:43

标签: 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]

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

我尝试了以下内容:

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表示CGPoint的{​​{1}},Location表示可能更改为的所有可能的CGPoints changedLoc组。我是随机计算的。

我有以下地点

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] 我如何删除allLocationsCGPoint(x: 421.0, y: 43.0) location1中的location2点?

2 个答案:

答案 0 :(得分:2)

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))

答案 1 :(得分:0)

令人困惑的是,array个实例中名为ptN的属性实际上是Dictionary<Int, [CGPoint]>

因此,对于您目前拥有的结构,您需要修改代码以匹配:

for pt in allPoints {
    for (key, ptArray) in pt.array    {
        for elem in ptArray   {
            //remove point from dict here
        }
    }
}