检查复合多维词典中存在密钥 [英] Checking If Key Exists in Complex Multidimensional Dictionary

查看:144
本文介绍了检查复合多维词典中存在密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将用户的位置和分配给他们的项目存储在不同的表中。认为它有用户正在销售的项目,并且项目的位置被分配给用户。



我使用radius来检索用户的位置。如果我使用10公里半径,我可以检索所有项目。但是,我想将项目分隔成每公里,例如:

   -  1km 
- 吉他
- 沙发
- 2公里
- 雪铁龙
- 雷诺
- 钢琴

问题是,查询的工作原理如此,所以如果我使用10km作为半径,它会从0到10公里获取所有内容:

  let circleQuery = geoFire.queryAtLocation(center,withRadius:10)

所以它日志如:

   -  1公里
- 吉他
- 沙发
- 2公里
- 吉他
- 沙发
- 雪铁龙
- 雷诺
- 钢琴






我以为可以使用一个for循环并将其存储到字典中。



方法我试过:

  var items = [Int:AnyObject]()

func retrieveItems() {

let center = CLLocation(纬度:userLatitude,longitude:userLongitude)

in 1 ... 25 {
let circleQuery = geoFire.queryAtLocation(center,withRadius:Double(index))

//在所有提取后

self。 item = [
index:[itemId:value]
]

//所以'index'将为km
//'itemId'是db
//'value'将是'key:value'

//数据库表看起来像:
// - itemId:
// key:值
// key:value
}
}

示例字典树:

  let dictionary = [
1:[
item1:[key :value,key:value],
item2:[key:val,key:val],
],
2:[
item3:[key:val,key:val],
item4:[key:val,key val],
item5:[key:val,key:val],
]
]

随着索引的增加,它从0km开始获取,所以重新获取已经获取的东西。但是,使用字典,也许我可以检查一下,如果'itemId'已经在字典中,不要重新添加。



我的问题是有没有办法检查是否已经使用上述方法在字典中添加了'itemId',所以我不添加已添加的项目?在复杂的多维字典中如何实现它的方法是什么?






更新:我尝试过这样的东西检查itemId是否存在

  if self.events.indexForKey(index)!= nil {
let radius = self.events [index]!如!字典< String,AnyObject>
print(km exists)
} else {
print(distance does not exist)
self.items = [
index:[itemId:价值]
]
}

但它始终在 else bit。

解决方案

如果您不想获得重复的事件,只需在25公里进行单个查询,检索所有密钥,然后使用客户端逻辑将这些查询过​​滤到各个km组中。您可以计算返回的键坐标与查询中心之间的距离使用一些数学


I am storing users' locations and items assigned to them in different tables. Think it as there are items user is selling and the location of the item is assigned to the user.

I am using radius to retrieve users' locations. I can retrieve all items if I use 10km radius. However, I want to separate items to each km, such as:

- 1km
  - Guitar
  - Sofa
- 2km 
  - Citroen
  - Renault
  - Piano

The problem is that the query works like this so if I use '10km' as radius, it fetches everything from 0 to 10km:

let circleQuery = geoFire.queryAtLocation(center, withRadius: 10)

So it logs like:

- 1km
  - Guitar
  - Sofa
- 2km 
  - Guitar
  - Sofa
  - Citroen
  - Renault
  - Piano


I thought one way could be using a for loop and storing it to a dictionary.

The approach I tried:

var items = [Int: AnyObject]()

func retrieveItems() {

  let center = CLLocation(latitude: userLatitude, longitude: userLongitude)

    for index in 1...25 {
        let circleQuery = geoFire.queryAtLocation(center, withRadius: Double(index))

       // after everything fetched

       self.items = [
           index : [itemId : value]
       ]

      // So, 'index' would be km 
      // 'itemId' is the id in db
      // 'value' would be 'key: value'

      // the database table looks like:
      //   - itemId:
      //      key:value
      //      key:value
    }
  }

Example dictionary tree:

 let dictionary = [
    1 : [
        "item1" : ["key": "value", "key": "value"],
        "item2" : ["key": "val", "key": "val"],
        ],
    2 : [
        "item3" : ["key": "val", "key": "val"],
        "item4" : ["key": "val", "key": "val"],
        "item5" : ["key": "val", "key": "val"],
    ]
]

As every time the index is increasing, it's fetching starting from 0km, so it's re-fetching already fetched stuff. But, using the dictionary, maybe I can make a check and if the 'itemId' is already in the dictionary, don't re-add it.

My question is, is there a way I can check if the 'itemId' is already added in the dictionary using above approach, so I don't add already added items? What is the way of achieving it in complex multidimensional dictionary like the one above?


Update: I tried something like this to check if the "itemId" exists

     if self.events.indexForKey(index) != nil {
         let radius = self.events[index]! as! Dictionary<String, AnyObject>
         print("km exists")
     } else {
         print("distance doesn't exist")
         self.items = [
             index : [itemId : value]
           ]            
     }

But it's always going inside else bit.

解决方案

If you don't want to get duplicate events, you should just do a single query at 25 km, retrieve all keys, and then use client-side logic to filter those out into individual km groups. You can compute the distance between the returned keys coordinates and the center of your query using some math.

这篇关于检查复合多维词典中存在密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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