如何从字典中删除某个CGPoint [英] How can I remove a certain CGPoint from a dictionaries

查看:198
本文介绍了如何从字典中删除某个CGPoint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一些循环字典的帖子,但在我看来,它们与我想要实现的不同而且简单。我有以下数组:

  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]

如何迭代去除 pt3.point 是来自所有 Int - [CGPoint] 字典的 allPoints



在pt.array中为pt {
为pt.arrays中的ptArrIndex {
$ b $为ptArrIndex中的(key,value){
//从此处的字典中删除点
}


$ / code $ / pre

但是我得到错误:$ b (key:Int,value:[CGPoint])'(aka'(key:Int,value:Array< CGPoint>)')的作用是
$ b

 不符合协议'序列'

在行:

$ b $ p $ for(key,value)in ptArrIndex {

编辑



创建每个点的结构如下所示:

$ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ var点:CGPoint
var arrays:[Int:[CGPoint]]
}

UPDATED QUESTION

基于Rob Napier的建议,我更新了这个问题:



  struct位置{
var point:CGPoint
var changedLoc:[Int:[CGPoint]]

其中表示 Location 和 changedLoc 表示所有可能的组 CGPoint CGPoints Location 可以变成。

我有以下地点:$ b​​
$ 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 解析方法

func locationsRemoving(changedLoc removePoint:CGPoint,from locations:[Location]) - > location.changedLoc {$ b $ return location.map {location in
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))


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?

解决方案

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

这篇关于如何从字典中删除某个CGPoint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆