0

我需要保留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解码并解码它的其他属性?AddressLocation

我事先不知道是否Address会作为可能需要持久化的属性应用于其他类型UserDefaults。我倾向于创建一个封装类型来进行编码和解码Address

4

1 回答 1

0

看来我应该创建一个封装类型来确保address我的Location实例的属性可以被编码和解码。这将允许我简单地调用coder.encode(address, forKey: "address").

更改搜索查询词后,我找到了有用的答案。

于 2017-05-05T17:03:44.067 回答