我应该对可以从JSON解析的对象模型的属性使用optional吗? [英] Should I use optional for properties of object models that will be parsed from JSON?

查看:569
本文介绍了我应该对可以从JSON解析的对象模型的属性使用optional吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iOS应用程序有一个非常常见的设置:它向使用JSON对象响应的API服务器进行HTTP查询。然后将这些JSON对象解析为适当的Swift对象。

My iOS app has a pretty common setup: it makes HTTP queries to an API server that responds with JSON objects. These JSON objects are then parsed to appropriate Swift objects.

最初,我将属性划分为必需的属性和可选属性,主要基于我的API服务器的数据库要求。例如, id email name 是必需的字段,因此他们使用非可选类型。其他可以在数据库中 NULL ,因此它们是可选类型。

Initially I divided properties into required properties and optional properties, mostly based on my API server's database requirements. For example, id, email, and name are require fields so they use non-optional types. Others can be NULL in database, so they are optional types.

class User {
  let id: Int
  let email: String
  let profile: String?
  let name: String
  let motive: String?
  let address: String?
  let profilePhotoUrl: String?
}

最近,我开始怀疑这是否是一个很好的设置。我发现虽然某些属性可能总是在数据库中,但这并不意味着这些属性将始终包含在JSON响应中。

Recently, I started wondering whether this was a good setup at all. I found out that although some properties might be always in the database, that does not mean that those properties will always be included in the JSON response.

例如,在用户个人资料页面中,需要所有这些字段才能正确显示视图。因此,JSON响应将包括所有这些字段。但是,对于列出用户名称的视图,我不需要电子邮件 id ,并且JSON响应应该可能不包括那些属性。不幸的是,这会导致错误并在将JSON响应解析为Swift对象时使应用程序崩溃,因为应用程序需要 id email name 总是不为零。

For example, in the User profile page, all these fields are needed to properly display the view. Therefore, JSON response will include all these fields. For a view that lists users' names, however, I would not need email or id, and JSON response should probably not include those properties either. Unfortunately, this will cause error and crash the app when parsing JSON response into Swift object since the app expects id, email, name to be always not-nil.

我正在考虑将Swift对象的所有属性更改为选项,但感觉就像丢掉了这种特定语言功能的所有好处。此外,我还需要编写更多的代码行,以便在应用程序的其他位置解包所有这些选项。

I'm thinking of changing all properties of Swift objects into optionals, but it feels like throwing away all the benefits of this language-specific feature. Moreover, I will have to write many more lines of code to unwrap all these optionals somewhere else in the app anyway.

另一方面,JSON对象本质上是它们的性质与严格的静态类型和Swift的nil-checking不能很好地互操作,所以简单地接受这种烦恼可能会更好。

On the other hand, JSON objects are by their nature not very interoperable with strict static typing and nil-checking of Swift so it might be better to simply accept that annoyance.

我是否应该转换为每个属性作为选项的模型?或者,还有更好的方法?我很感激这里有任何评论。

Should I transition to models with every property as optionals? Or is there a better way? I'd appreciate any comment here.

推荐答案

有三种方法可以解决这个问题:

There are three ways you can go with this:


  1. 始终发送所有JSON数据,并使您的属性不可选。

  1. Always send all the JSON data, and leave your properties non-optional.

使所有属性可选。

使所有属性都是非可选的,并编写自己的 init(来自:)方法进行分配默认值为缺失值,如此答案中所述。

Make all the properties non-optional, and write your own init(from:) method to assign default values to missing values, as described in this answer.

所有这些都应该有效;哪一个是最好的是基于意见的,因此超出了Stack Overflow答案的范围。选择最适合您特定需求的那一个。

All of these should work; which one is "best" is opinion-based, and thus out of the scope of a Stack Overflow answer. Choose whichever one is most convenient for your particular need.

这篇关于我应该对可以从JSON解析的对象模型的属性使用optional吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆