gpt4 book ai didi

ios - Swift Codable-如何编码自定义数组

转载 作者:行者123 更新时间:2023-12-01 18:35:22 26 4
gpt4 key购买 nike

那是我的JSON案例

      {
"image_id": 11101,
"image_source_id": 9,
"image_author": "",
"image_copyright": "",
"image_format_list": [{
"image_format": {
"image_url": "https://static.musixmatch.com/images-storage/mxmimages/1/0/1/1/1/11101_2.jpg",
"image_format_id": 2,
"width": 150,
"height": 150
}
},
{
"image_format": {
"image_url": "https://static.musixmatch.com/images-storage/mxmimages/1/0/1/1/1/11101_16.jpg",
"image_format_id": 16,
"width": 451,
"height": 500
}
}
]
}

我可以在两个不同的类中正确解码我的自定义对象:MXMImage和MXMImageFormat。
但我不知道如何重新编码我的对象以重建相同的JSON

那是我的代码:

    struct MXMImage : Decodable, Encodable, Equatable {
let imageId: Int
let imageSourceId: Int
let imageAuthor: String?
let imageCopyright: String?
let imageFormatList: [MXMImageFormat]?

enum CodingKeys: String, Swift.CodingKey {
case imageId
case imageSourceId
case imageAuthor
case imageCopyright
case imageFormatList

enum ImageFormatListKey: String, CodingKey {
case imageFormat
}
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
imageId = try (container.decodeIfPresent(Int.self, forKey: .imageId) ?? 0)
imageSourceId = try (container.decodeIfPresent(Int.self, forKey: .imageSourceId) ?? 0)
imageAuthor = try? container.decodeIfPresent(String.self, forKey: .imageAuthor)
imageCopyright = try? container.decodeIfPresent(String.self, forKey: .imageCopyright)

var imagesFormatListContainer = try container.nestedUnkeyedContainer(forKey: .imageFormatList)
var imagesList:[MXMImageFormat] = []
while !imagesFormatListContainer.isAtEnd {
let imageFormatContainer = try imagesFormatListContainer.nestedContainer(keyedBy: CodingKeys.ImageFormatListKey.self)
let imageFormat = try? imageFormatContainer.decode(MXMImageFormat.self, forKey: .imageFormat)
if let imageFormat = imageFormat {
imagesList.append(imageFormat)
}
}
self.imageFormatList = imagesList
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encodeIfPresent(imageId, forKey: .imageId)
try container.encodeIfPresent(imageSourceId, forKey: .imageSourceId)
try container.encodeIfPresent(imageAuthor, forKey: .imageAuthor)
try container.encodeIfPresent(imageCopyright, forKey: .imageCopyright)

var imageContainer = container.nestedUnkeyedContainer(forKey: .imageFormatList)
try imageFormatList?.forEach { imgFormat in
var nested = imageContainer.nestedContainer(keyedBy: CodingKeys.ImageFormatListKey.self)
let data = try imgFormat.encoded()
try nested.encode(data, forKey: .imageFormat)

}
}
}


特别是,我不知道如何在 image_format键内重新缩进MXMImageFormat对象,然后对自定义数组进行编码。有可能这样做吗?提前致谢

最佳答案

除了nestedContainers外,您还可以解码/编码[[String:MXMImageFormat]]数组并将其映射

struct MXMImage : Codable, Equatable {
let imageId: Int
let imageSourceId: Int
let imageAuthor: String?
let imageCopyright: String?
let imageFormatList: [MXMImageFormat]?

private enum CodingKeys : String, CodingKey { case imageId, imageSourceId, imageAuthor, imageCopyright, imageFormatList}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
imageId = try container.decode(Int.self, forKey: .imageId)
imageSourceId = try container.decode(Int.self, forKey: .imageSourceId)
imageAuthor = try container.decodeIfPresent(String.self, forKey: .imageAuthor)
imageCopyright = try container.decodeIfPresent(String.self, forKey: .imageCopyright)
if let imageFormatListData = try container.decodeIfPresent([[String:MXMImageFormat]].self, forKey: .imageFormatList) {
imageFormatList = imageFormatListData.compactMap{$0["image_format"]}
} else {
imageFormatList = nil
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(imageId, forKey: .imageId)
try container.encode(imageSourceId, forKey: .imageSourceId)
try container.encodeIfPresent(imageAuthor, forKey: .imageAuthor)
try container.encodeIfPresent(imageCopyright, forKey: .imageCopyright)
if let imageFormatListData = imageFormatList {
try container.encode(imageFormatListData.map{["image_format":$0]}, forKey: .imageFormatList)
}
}
}

struct MXMImageFormat : Codable, Equatable {
let imageUrl : URL
let imageFormatId, width, height : Int
}

关于ios - Swift Codable-如何编码自定义数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60231030/

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