11
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CodableでのValueObject利用

Posted at

iOSDC Japan 2019にて、@maguhiro による素晴らしいLTがありました。
値オブジェクトへのCodable対応 by maguhiro | トーク | iOSDC Japan 2019 #iosdc - fortee.jp

この投稿では、筆者が同様の実装をする場合によくやる方法を、別解として紹介します。

実装

struct User: Codable {
    var id: UserID
    var name: String
}

struct UserID: Codable, RawRepresentable {
    var rawValue: Int
    
    init(rawValue: Int) {
        self.rawValue = rawValue
    }
}

do {
    let json = """
    {
        "id": 1,
        "name": "administrator"
    }
    """.data(using: .utf8)!
    
    let user = try JSONDecoder().decode(User.self, from: json)
    print("id: \(user.id.rawValue), name: \(user.name)") // id: 1, name: administrator
} catch {
    print(error)
}

do {
    let user = User(id: .init(rawValue: 2), name: "member")
    
    let json = try JSONEncoder().encode(user)
    print(String(data: json, encoding: .utf8)!) // {"id":2,"name":"member"}
} catch {
    print(error)
}

解説

RawRepresentableRawValue特定の型だった場合、Encodable, Decodableのデフォルト実装がされています。

これを利用することで、init(from:), encode(to:) を自分で実装する必要が無くなります。
デフォルト実装の内容は @maguhiro のスライドのものと同様ですね。

11
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?