Error[8]: Undefined offset: 3, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述为什么名称Array不能解码? 准备好游乐场,简单地将它粘贴到你的 *** 场上 import Foundationstruct Country : Decodable { enum CodingKeys : String, CodingKey { case names } var names : [String]?}extension Country 为什么名称Array不能解码?

准备好游乐场,简单地将它粘贴到你的 *** 场上

import Foundationstruct Country : Decodable {    enum CodingKeys : String,CodingKey {        case names    }    var names : [String]?}extension Country {    public init(from decoder: Decoder) throws {        let values = try decoder.container(keyedBy: CodingKeys.self)        names = try values.decode([String]?.self,forKey: .names)!    }}let Json = """ [{    "names":      [       "Andorre","Andorra","アンドラ"      ] },{    "names":      [       "United arab Emirates","Vereinigte arabische Emirate","Émirats arabes Unis","Emiratos Árabes UnIDos","アラブ首長国連邦","Verenigde arabische Emiraten"      ]  }]""".data(using: .utf8)!let decoder = JsONDecoder()do {    let countrIEs = try decoder.decode([Country].self,from: Json)    countrIEs.forEach { print(
extension Country {    public init(from decoder: Decoder) throws {        let values = try decoder.container(keyedBy: CodingKeys.self)        names = try values.decodeIfPresent([String].self,forKey: .names)    }}
) }} catch { print("error")}
解决方法 您已将名称定义为Country的可选属性.
如果您的意图是该密钥可能不存在于JsON中
然后使用decodeIfPresent:

} catch {    print(error.localizedDescription)}

如果容器没有与key关联的值,或者值为null,则此方法返回nil.

但实际上你可以省略你的自定义init(来自解码器:解码器)
实现(和枚举CodingKeys),因为这是默认行为而且会
自动合成.

备注:在任何catch子句中定义了隐式变量错误,
所以

[+++]

可以提供更多信息,而不仅仅是一个打印(“错误”)(虽然不是在这个特殊的情况下).

总结

以上是内存溢出为你收集整理的swift – Codable / Decodable应该使用字符串解码数组全部内容,希望文章能够帮你解决swift – Codable / Decodable应该使用字符串解码数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
swift – CodableDecodable应该使用字符串解码数组_app_内存溢出

swift – CodableDecodable应该使用字符串解码数组

swift – CodableDecodable应该使用字符串解码数组,第1张

概述为什么名称Array不能解码? 准备好游乐场,简单地将它粘贴到你的 *** 场上 import Foundationstruct Country : Decodable { enum CodingKeys : String, CodingKey { case names } var names : [String]?}extension Country 为什么名称Array不能解码?

准备好游乐场,简单地将它粘贴到你的 *** 场上

import Foundationstruct Country : Decodable {    enum CodingKeys : String,CodingKey {        case names    }    var names : [String]?}extension Country {    public init(from decoder: Decoder) throws {        let values = try decoder.container(keyedBy: CodingKeys.self)        names = try values.decode([String]?.self,forKey: .names)!    }}let Json = """ [{    "names":      [       "Andorre","Andorra","アンドラ"      ] },{    "names":      [       "United arab Emirates","Vereinigte arabische Emirate","Émirats arabes Unis","Emiratos Árabes UnIDos","アラブ首長国連邦","Verenigde arabische Emiraten"      ]  }]""".data(using: .utf8)!let decoder = JsONDecoder()do {    let countrIEs = try decoder.decode([Country].self,from: Json)    countrIEs.forEach { print(
extension Country {    public init(from decoder: Decoder) throws {        let values = try decoder.container(keyedBy: CodingKeys.self)        names = try values.decodeIfPresent([String].self,forKey: .names)    }}
) }} catch { print("error")}
解决方法 您已将名称定义为Country的可选属性.
如果您的意图是该密钥可能不存在于JsON中
然后使用decodeIfPresent:

} catch {    print(error.localizedDescription)}

如果容器没有与key关联的值,或者值为null,则此方法返回nil.

但实际上你可以省略你的自定义init(来自解码器:解码器)
实现(和枚举CodingKeys),因为这是默认行为而且会
自动合成.

备注:在任何catch子句中定义了隐式变量错误,
所以

可以提供更多信息,而不仅仅是一个打印(“错误”)(虽然不是在这个特殊的情况下).

总结

以上是内存溢出为你收集整理的swift – Codable / Decodable应该使用字符串解码数组全部内容,希望文章能够帮你解决swift – Codable / Decodable应该使用字符串解码数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1003204.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存