- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试将“Object Mapper”转换为“Codable”。我来自服务的响应包括 NSArray
,其中包含自定义对象。我需要在 Codable 类中使用 NSArray
和 NSDictionary
。但是,我失败了。我尝试使用像 AnyCodable 这样的第三方库,但是,我又失败了。我无法更改服务器端的响应。它必须以 Array 的形式出现。我必须使用数组。您有什么建议或信息吗?
class Person : Codable { //Error 1
var name: String?
var data: NSArray?
private enum CodingKeys : String, CodingKey {
case name
case data
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
try container.encode(data, forKey: .data) //Error 2
}
func decode(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name)
self.data = try container.decode(NSArray.self, forKey: .data) //Error 3
}
}
Error 1: "Type 'Person' does not conform to protocol 'Decodable'"
Error 2: "Argument type 'NSArray' does not conform to expected type 'Encodable'"
Error 3: "Instance method 'decode(_:forKey:)' requires that 'NSArray' conform to 'Decodable'"
示例响应在这里。数组中的所有项目都不具有相同的内容。数组中的每一项都有不同的类型。
{
"data": [
{
"languageCode": "EN",
"deviceInformation": {
"screenSize": "height:812.0 width:375.0",
"connectionType": "wifi",
"deviceType": "iPhone",
"deviceCode": "D01D304C-D05C-4443-9A92-031C55D14XC7",
"operatingSystemVersion": "12.2",
"applicationVersion": "1.0"
},
"lastUpdatedParamDate": "13.05.2019 14:44:24",
"skipOptionalUpdate": 0
}
]
}
最佳答案
当你成功的时候
var data: NSArray?
这是一个桥接目标 - c 类型具有 Any
作为元素的类型,并且 Any 不符合 Codable
因此明确地将其设为类似
var data:[SomeModel]
或者使用 JSONSerialization
代替
struct Root: Codable {
let data: [Model]
}
struct Model: Codable {
let languageCode: String
let deviceInformation: DeviceInformation
let lastUpdatedParamDate: String
let skipOptionalUpdate: Int
}
struct DeviceInformation: Codable {
let screenSize, connectionType, deviceType, deviceCode: String
let operatingSystemVersion, applicationVersion: String
}
let eventData = try JSONDecoder().decode(Root.self, from: data)
if you have more attributes you can add them , if some returns
nil
in some cases make them optional , but if the type changes then no way with Codable
关于ios - 可以在 Codable 中使用 NSArray 吗? (将 "Object Mapper"转换为 "Codable"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56148126/
from sqlalchemy import * from sqlalchemy import create_engine, ForeignKey from sqlalchemy import Col
我使用 MR1 API(hadoop-core-1.2.1.jar) 编写了一个示例字数统计程序。映射器类定义如下, public interface Mapper extends JobConfi
创建maven项目,修改pom.xml文件,如下: ? 1
问题最终得到解决在底部查看我的解决方案 最近我正在尝试运行 Mahout in Action 的第 6 章( list 6.1 ~ 6.4)中的推荐系统示例。但是我遇到了一个问题,我已经用谷歌搜索了但
如果有人知道在 .NET 中执行此操作的更多方法,您对这些方法有何看法?您选择哪种方法,为什么? 下面是.NET中对象拷贝不同方式的测试。 与此原始线程相关的测试:How to copy value
我能看出参数个数的不同,但我不知道实现上的不同。 每种方法的行为是否存在重要差异? 最佳答案 第一个填充您传入的现有对象。第二个为您创建一个新对象。这是“项目”和“填充”之间的语义差异。 关于c# -
我正在将一个项目从 Glass Mapper v2 (Glass.Mapper.Sitecore) 升级到 v4 (Glass.Mapper.Sc),我遇到了一个问题,我们的解决方案是使用 Insta
我正在实现一些 hadoop 应用程序。我的编码部分几乎完成了。但是想在阅读“Lin & Chris Dryer”的映射器设计模式书后改进编码器。至于这种方法的有效实现,需要在 map 函数中保留状态
当我尝试访问该页面时发生此错误。我在创建表时没有遇到错误,但似乎仍然存在问题。 模型是这样的: class User(UserMixin, db.Model): id = db.Column(
我创建了一个使用 SQLAlchemy 的类: class DbAbsLayer(object): def __init__(self): self.setConnection
我试图在我的 MVC - Sitecore - 7.1 中的 v4.0.30319 项目中使用 Glass Mapper。 以下是我安装的 Glass Mapper 版本 Glass Mapper 版
我在尝试使用 org.apache.hadoop.mapred.lib.IdentityMapper 作为 Hadoop Streaming 1.0.3 中 -mapper 的参数时遇到了问题。 “猫
这是我的mybatis配置。这是我的pom.xml。。当我运行项目时,它显示了错误的原因:org/mybatis/spring/mapper/MapperScannerConfigurer.有没有人能
背景: 1.在数据库中有一个通知表 可以看到其中的 gmt_create、 notifier_name、 outer_title 这三个字段是有下划线的 2.这张表
hadoop配置对象仅允许在set方法中将字符串作为值 set(字符串名称,字符串值) 是否有一种简单的方法来设置任何其他对象类型?我想在映射器中检索这些对象。 我注意到在0.15左右的版本中,有一个
我要开发的更大目标如下: a)仪表板,除其他功能外,用户还可以上传文档(.pdf,.txt,.doc)。所有这些文档都转到特定目录。 b)用户还可以查询所有带有特定关键字标记的文档。 现在,我希望使用
2016-10-20 18:03:51.253 WARN 17216 --- [restartedMain] .s.c.a.CommonAnnotationBeanPostProcessor:在名为“
我正在尝试同时使用映射器和缩减器来运行作业,但映射器运行缓慢.. 如果对于相同的输入我禁用 reducers,映射器将在 3 分钟内完成而对于 mapper-reducer 作业,即使在 30 分钟后
由于一些数据在所有 map() 函数之间共享,我无法在 setup() 中生成它们,因为每个 setup() 对应于每个map() 函数,而我想做的是预先生成一些数据并将其存储在可实现的地方,然后在每
我正在使用 Hadoop,我想使用静态变量来减少必须进行的方法调用次数。以下是我如何使用静力学: public class Mapper extends Mapper { protected
我是一名优秀的程序员,十分优秀!