gpt4 book ai didi

swift - 存储无序数据文件中的值

转载 作者:行者123 更新时间:2023-11-28 12:06:18 25 4
gpt4 key购买 nike

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

实际文件是一个 GeoJSON 文件,其中包含高尔夫球洞特征的 map 坐标,其中定义了许多 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 个 map 坐标,外加至少 3 个多边形,但可能更多。

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

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

我是 Swift 的新手,正在学习 Swift,所以我是否错过或误解了上述数据类型的某些内容?有没有一种我可能没有听说过的方法可以实现上述目标?

最佳答案

[Swift 4] 根据您的 Json 结构,您必须使用 Codable 设计您的模型类/结构。根据您的示例,我发现 4 个模型结构足以将您的 json 映射到一个对象。

首先创建 RootModel 结构,它将包含另一个结构,例如 Feature:

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

}

现在您需要 Feature 模型结构,它将包含 PropertiesGeometry:

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

关于swift - 存储无序数据文件中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49310457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com