存储无序数据文件中的值

时间:2018-03-15 22:47:22

标签: swift mapbox geojson

解析/循环遍历无序的数据文件时,如何将值存储到对象或变量中以供以后使用?

实际文件是一个GeoJSON文件,其中包含高尔夫球洞特征的地图坐标,其中有许多地理对象,其中定义了2种类型(多边形或点)。以下是一个小样本:

{
  "features": [
    {
      "type": "Feature",
      "properties": {
        "holeno": 19,
        "feature": "teebox"
      },
      "geometry": {
        "coordinates": [
          [
            [
              -1.478163,
              53.869862
            ],
            [
              -1.478122,
              53.869801
            ],
            [
              -1.477888,
              53.869859
            ],
            [
              -1.477927,
              53.869922
            ],
            [
              -1.478163,
              53.869862
            ]
          ]
        ],
        "type": "Polygon"
      },
      "id": "0351f53178c564588a506709c7039509"
    },
    {
      "type": "Feature",
      "properties": {
        "holeno": 15,
        "feature": "pastGreen"
      },
      "geometry": {
        "coordinates": [
          -1.472843,
          53.871483
        ],
        "type": "Point"
      },
      "id": "03850283164d2a7a63d1793baebff719"
    },
    {
      "type": "Feature",
      "properties": {
        "holeno": 8,
        "feature": "teebox"
      },
      "geometry": {
        "coordinates": [
          [
            [
              -1.479439,
              53.875594
            ],
            [
              -1.47972,
              53.875493
            ],
            [
              -1.479667,
              53.875434
            ],
            [
              -1.479363,
              53.87554
            ],
            [
              -1.479439,
              53.875594
            ]
          ]
        ],
        "type": "Polygon"
      },
      "id": "05a1644f6c7d11db4f802fb14b98b8b3"
    }
],
  "type": "FeatureCollection"
}

我希望能够将这些存储到类,变量或对象中以供以后使用/处理。但无序结构给我带来了问题,特别是在选择数据类型和初始化方面。

数组有自己的索引,从0开始,你不能使用insert(at :)函数插入一个随机数。

字典仅存储单个键值对。我每洞有2个地图坐标,加上至少3个多边形,但可能更多。

结构体具有更简单的初始化,但在创建每个结构时,我只会知道1个属性的值。我可以为每个属性使用可选值,但是建议的方法是什么来完成实例的所有属性的初始化?我如何检查先前是否已创建特定孔的实例?

一个类与结构具有相同的问题,但在初始化方面更复杂。

我是新手并学习Swift,所以我是否错过或误解了上述数据类型的内容?有没有办法完成上述我可能没有听说过?

1 个答案:

答案 0 :(得分:2)

[Swift 4] 基于您的Json结构,您必须使用 Codable 设计模型类/结构。根据你的样本,我找到了4个模型结构,足以将你的json映射到一个对象。

首先创建 RootModel 结构,它将包含另一个结构功能

    struct RootModel : Codable {
     let features : [Features]?
     let type : String?

     enum CodingKeys: String, CodingKey {

     case features = "features"
     case type = "type"
    }

    init(from decoder: Decoder) throws {
      let values = try decoder.container(keyedBy: CodingKeys.self)
      features = try values.decodeIfPresent([Features].self, forKey: .features)
      type = try values.decodeIfPresent(String.self, forKey: .type)
    }

    }

现在您需要功能模型结构,它将保留属性&的几何

struct Features : Codable {
let type : String?
let properties : Properties?
let geometry : Geometry?
let id : String?

enum CodingKeys: String, CodingKey {

    case type = "type"
    case properties
    case geometry
    case id = "id"
}

init(from decoder: Decoder) throws {
    let values = try decoder.container(keyedBy: CodingKeys.self)
    type = try values.decodeIfPresent(String.self, forKey: .type)
    properties = try Properties(from: decoder)
    geometry = try Geometry(from: decoder)
    id = try values.decodeIfPresent(String.self, forKey: .id)
}

}

几何&属性struct:

struct Properties : Codable {
let holeno : Int?
let feature : String?
----- similar CodingKeys & init----
}

struct Geometry : Codable {
let coordinates : [[[Double]]]?
let type : String?
----- similar CodingKeys & init----
init(from decoder: Decoder) throws {
    let values = try decoder.container(keyedBy: Features.CodingKeys.self)
    let geoValues = try values.nestedContainer(keyedBy: CodingKeys.self, forKey: .geometry)
    type = try geoValues.decodeIfPresent(String.self, forKey: .type)
    if type == "Point" {
        let pointVal = try geoValues.decodeIfPresent([Double].self, forKey: .coordinates)
        let nestedVal = [[pointVal]]
        coordinates = nestedVal as? [[[Double]]]
    } else {
        coordinates = try geoValues.decodeIfPresent([[[Double]]].self, forKey: .coordinates)

    }
}
}

最后,只需使用 RootModel 结构,如下所示:

let data: Data? = your_json_string.data(using: .utf8)
let jsonDecoder = JSONDecoder()
let responseModel = try! jsonDecoder.decode(RootModel.self, from: data!)