【发布时间】:2017-05-05 14:19:46
【问题描述】:
我需要保留用户选择的首选Location。 Location 包含一些属性:
public struct Location {
// MARK: - Properties
public let identifier: Int
public let name: String
public let address: Address?
// MARK: - Initializations
public init(identifier: Int, name: String, address: Address?) {
self.identifier = identifier
self.name = name
self.address = address
}
}
Address 如下:
public struct Address {
// MARK: - Properties
public let city: String?
public let state: String?
public let postalCode: String?
public let country: String?
// MARK: - Initialization
public init(city: String?, state: String?, postalCode: String?, country: String?) {
self.city = city
self.state = state
self.postalCode = postalCode
self.country = country
}
}
由于我只需要在任何给定时间坚持一个Location,我更喜欢使用UserDefaults。
我有一个封装Location 的类型,以便可以对其进行编码和解码,以便由UserDefaults 持久化。但是,我还没有创建用于编码和解码Address 的封装类型。
我的问题是:由于我想保留一个包含Address 的Location,我是否还需要创建封装类型来对Address 进行编码和解码,还是更适合当我编码和解码它的其他属性时,只需编码和解码Location 内的Address 属性?
我事先不知道Address 是否会作为可能需要在UserDefaults 中持久化的属性应用于其他类型。我倾向于创建一个封装类型来编码和解码Address。
【问题讨论】:
标签: struct encoding decoding userdefaults