我需要保留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
.
我有一种封装 a 的类型,Location
以便可以对其进行编码和解码,以便由UserDefaults
. 但是,我还没有为编码和解码创建封装类型Address
。
我的问题是:由于我想保留Location
包含 a 的 a ,我Address
是否需要创建封装类型来编码和Address
解码并解码它的其他属性?Address
Location
我事先不知道是否Address
会作为可能需要持久化的属性应用于其他类型UserDefaults
。我倾向于创建一个封装类型来进行编码和解码Address
。