Skip to content

Commit bf0147b

Browse files
committed
feat: JSON 디코딩에 사용할 구조체에 디코딩을 구현하였습니다 (#4)
1 parent feb4dcb commit bf0147b

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

Client/iOS/TodoList/Network/ResourcesNetwork/BoardData.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,29 @@ struct BoardData: Codable
1111
{
1212
var objectId: String
1313
var boardName: String
14+
15+
init(from decoder: Decoder) throws {
16+
let container = try decoder.container(keyedBy: BoardDataJsonKeys.self)
17+
18+
guard let objectId = try? container.decode(String.self, forKey: .objectId) else {
19+
throw BoardDataDecodedError.objectIdError
20+
}
21+
22+
guard let boardName = try? container.decode(String.self, forKey: .boardName) else {
23+
throw BoardDataDecodedError.boardNameError
24+
}
25+
26+
self.objectId = objectId
27+
self.boardName = boardName
28+
}
29+
30+
enum BoardDataJsonKeys: String, CodingKey {
31+
case objectId = "objectId"
32+
case boardName = "boardName"
33+
}
34+
35+
enum BoardDataDecodedError: String, Error {
36+
case objectIdError = "objectId 값 디코딩에 실패하였습니다."
37+
case boardNameError = "boardName 값 디코딩에 실패하였습니다."
38+
}
1439
}

Client/iOS/TodoList/Network/ResourcesNetwork/CardData.swift

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,70 @@ import Foundation
1010
struct CardData: Codable
1111
{
1212
var objectId: String
13-
1413
var title: String
1514
var contents: String
1615
var creationDate: String
1716
var index: Int
1817
var status: Int
1918
var updateDate: String
19+
20+
init(from decoder: Decoder) throws {
21+
let container = try decoder.container(keyedBy: CardDataJsonKeys.self)
22+
23+
guard let objectId = try? container.decode(String.self, forKey: .objectId) else {
24+
throw CardDataDecodedError.objectIdError
25+
}
26+
27+
guard let title = try? container.decode(String.self, forKey: .title) else {
28+
throw CardDataDecodedError.titleError
29+
}
30+
31+
guard let contents = try? container.decode(String.self, forKey: .contents) else {
32+
throw CardDataDecodedError.contentsError
33+
}
34+
35+
guard let creationDate = try? container.decode(String.self, forKey: .creationDate) else {
36+
throw CardDataDecodedError.creationDateError
37+
}
38+
39+
guard let index = try? container.decode(Int.self, forKey: .index) else {
40+
throw CardDataDecodedError.indexError
41+
}
42+
43+
guard let status = try? container.decode(Int.self, forKey: .status) else {
44+
throw CardDataDecodedError.statusError
45+
}
46+
47+
guard let updateDate = try? container.decode(String.self, forKey: .updateDate) else {
48+
throw CardDataDecodedError.updateDateError
49+
}
50+
51+
self.objectId = objectId
52+
self.title = title
53+
self.contents = contents
54+
self.creationDate = creationDate
55+
self.index = index
56+
self.status = status
57+
self.updateDate = updateDate
58+
}
59+
60+
enum CardDataJsonKeys: String, CodingKey {
61+
case objectId = "objectId"
62+
case title = "title"
63+
case contents = "contents"
64+
case creationDate = "creationDate"
65+
case index = "index"
66+
case status = "status"
67+
case updateDate = "updateDate"
68+
}
69+
70+
enum CardDataDecodedError: String, Error {
71+
case objectIdError = "objectId 값 디코딩에 실패하였습니다."
72+
case titleError = "title 값 디코딩에 실패하였습니다."
73+
case contentsError = "contents 값 디코딩에 실패하였습니다."
74+
case creationDateError = "creationDate 값 디코딩에 실패하였습니다."
75+
case indexError = "index 값 디코딩에 실패하였습니다."
76+
case statusError = "status 값 디코딩에 실패하였습니다."
77+
case updateDateError = "updateDate 값 디코딩에 실패하였습니다."
78+
}
2079
}

Client/iOS/TodoList/Network/ResourcesNetwork/HistoryData.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,29 @@ struct HistoryData: Codable
1111
{
1212
var objectId: String
1313
var activityType: String
14+
15+
init(from decoder: Decoder) throws {
16+
let container = try decoder.container(keyedBy: HistoryDataJsonKeys.self)
17+
18+
guard let objectId = try? container.decode(String.self, forKey: .objectId) else {
19+
throw HistoryDataDecodedError.objectIdError
20+
}
21+
22+
guard let activityType = try? container.decode(String.self, forKey: .activityType) else {
23+
throw HistoryDataDecodedError.objectIdError
24+
}
25+
26+
self.objectId = objectId
27+
self.activityType = activityType
28+
}
29+
30+
enum HistoryDataJsonKeys: String, CodingKey {
31+
case objectId = "objectId"
32+
case activityType = "activityType"
33+
}
34+
35+
enum HistoryDataDecodedError: String, Error {
36+
case objectIdError = "objectId 값 디코딩에 실패하였습니다."
37+
case activityTypeError = "activityType 값 디코딩에 실패하였습니다."
38+
}
1439
}

0 commit comments

Comments
 (0)