Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 将[[[Double]]]抛回[Double]_Arrays_Swift_Casting_Codable - Fatal编程技术网

Arrays 将[[[Double]]]抛回[Double]

Arrays 将[[[Double]]]抛回[Double],arrays,swift,casting,codable,Arrays,Swift,Casting,Codable,我有一个下载的JSON文件。其中有两种类型中的一种类型的重复对象,即[Double]或[[Double]]] 我正在尝试使用自定义结构的可编码协议将数据转储到对象中。为了解决上述问题,我实际上已经将更简单的[Double]转换为相同的[[[Double]]]类型 稍后,在使用这些数据时,我正在努力将其转换回更简单的单层阵列。我希望我能强制将这一点回溯到单曲as![双精度]类型。我还能怎么做?每个阵列层是否需要一个“for-in”循环 或者,如何调整我的几何体结构,使我不必为该属性处理不同类型的数

我有一个下载的JSON文件。其中有两种类型中的一种类型的重复对象,即
[Double]
[[Double]]]

我正在尝试使用自定义结构的可编码协议将数据转储到对象中。为了解决上述问题,我实际上已经将更简单的
[Double]
转换为相同的
[[[Double]]]
类型

稍后,在使用这些数据时,我正在努力将其转换回更简单的单层阵列。我希望我能强制将这一点回溯到单曲
as![双精度]
类型。我还能怎么做?每个阵列层是否需要一个“for-in”循环

或者,如何调整我的几何体结构,使我不必为该属性处理不同类型的数组?我想知道
coordinates
属性是否可以更改为
Any
类型或其他类型

struct Geometry: Codable {
    let coordinates: [[[Double]]]
    let type: String

    enum CodingKeys: String, CodingKey {
        case coordinates
        case type
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        type = try container.decode(String.self, forKey: .type)

        if type == "Point" {
            let typeFromMapbox = try container.decode([Double].self, forKey: .coordinates)
            coordinates = [[typeFromMapbox]]
        } else { // THIS IS A POLYGON
            coordinates = try container.decode([[[Double]]].self, forKey: .coordinates)
        }
    }
}
我只是开始和学习与斯威夫特

感谢您的帮助、指点或见解


谢谢你没有提供实际的JSON。所以,我假设它是:

{
    "geometries" :
    [
        {
            "coordinates" : [1.0, 2.0],
            "type" : "flat"
        },
        {
            "coordinates" : [[[111.0, 222.0]]],
            "type" : "multi"
        }
    ]
}
基于上述结构,您的根级别数据类型将是:

struct Root: Codable {
    let geometries: [Geometry]
}
然后,您的
几何体将被定义为:

struct Geometry: Codable {
    // As long as your coordinates should be at least a flat array. Multi dimensional array will be handled by `Coordinate` type
    let coordinates: [Coordinate]
    let type: String
}
现在是
坐标
数组。这可能是
Double
类型或
[[Double]]
类型。因此,请使用
enum
将其包装起来:

enum Coordinate: Codable {
    case double(Double)
    case arrayOfDoubleArray([[Double]])

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        do {
            self = try .double(container.decode(Double.self))
        } catch DecodingError.typeMismatch {
            do {
                self = try .arrayOfDoubleArray(container.decode([[Double]].self))
            } catch DecodingError.typeMismatch {
                throw DecodingError.typeMismatch(Coordinate.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Coordinate type doesn't match"))
            }
        }
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .double(let double):
            try container.encode(double)
        case .arrayOfDoubleArray(let arrayOfDoubleArray):
            try container.encode(arrayOfDoubleArray)
        }
    }
}
只要JSON中的
键和
结构中的属性相同,就不需要提供
编码键

现在解码:
您没有提供实际的JSON。所以,我假设它是:

{
    "geometries" :
    [
        {
            "coordinates" : [1.0, 2.0],
            "type" : "flat"
        },
        {
            "coordinates" : [[[111.0, 222.0]]],
            "type" : "multi"
        }
    ]
}
基于上述结构,您的根级别数据类型将是:

struct Root: Codable {
    let geometries: [Geometry]
}
然后,您的
几何体将被定义为:

struct Geometry: Codable {
    // As long as your coordinates should be at least a flat array. Multi dimensional array will be handled by `Coordinate` type
    let coordinates: [Coordinate]
    let type: String
}
现在是
坐标
数组。这可能是
Double
类型或
[[Double]]
类型。因此,请使用
enum
将其包装起来:

enum Coordinate: Codable {
    case double(Double)
    case arrayOfDoubleArray([[Double]])

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        do {
            self = try .double(container.decode(Double.self))
        } catch DecodingError.typeMismatch {
            do {
                self = try .arrayOfDoubleArray(container.decode([[Double]].self))
            } catch DecodingError.typeMismatch {
                throw DecodingError.typeMismatch(Coordinate.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Coordinate type doesn't match"))
            }
        }
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .double(let double):
            try container.encode(double)
        case .arrayOfDoubleArray(let arrayOfDoubleArray):
            try container.encode(arrayOfDoubleArray)
        }
    }
}
只要JSON中的
键和
结构中的属性相同,就不需要提供
编码键

现在解码:
发布JSON。这将使每个人都更容易帮助你。这会让每个人都更容易帮助你。@nayem,谢谢你的回复。您猜对了JSON格式,因此我不提供其他示例。在编码和解码过程中,您似乎做了更多的“工作”,而且这似乎并不比将这两种类型都保留为三元组并在需要时根据需要展开要省力多少。我主要是询问是否有一种特殊/快捷/首选的技术。@nayem,谢谢你的回复。您猜对了JSON格式,因此我不提供其他示例。在编码和解码过程中,您似乎做了更多的“工作”,而且这似乎并不比将这两种类型都保留为三元组并在需要时根据需要展开要省力多少。我主要是询问是否有特殊/快捷/首选的技术。