typealias Codable = Decodable & Encodable
Codable ์ ์ฑํํ๋ค = Class, Struct, Enum์ย serialize /ย deserializeํ ์ ์๋ค
Codable : Decodable ๊ณผ Encodable ์ ๋ชจ๋ ์ฑํ
Decodable : ์์ ์ ์ธ๋ถํํ์ผ๋ก ๋์ฝ๋ฉ ํ ์ ์๋ ํ์ (ex - JSON -> ์์ )
Encodable : ์์ ์ ์ธ๋ถํํ์ผ๋ก ์ธ์ฝ๋ฉ ํ ์ ์๋ ํ์ (ex - ์์ -> JSON)
PropertyListEncoder ๋ JSONEncoder ํด๋์ค๋ก ์ธ์ฝ๋ฉ์ ํ ์ ์๋ค.
.encode
๋ฉ์๋๋ Encodable ํ์
์ ์ค์ํ๋ T ๋ฅผ JSON Data ๋ก ๋ฆฌํดํด์ค๋ค๐ ์์๋ฅผ ๋ณด์!
1. Codable ํ๋กํ ์ฝ์ ์ฑํํ Car ๊ตฌ์กฐ์ฒด๊ฐ ์๋ค
struct Car: Codable {
let name: String
let brand: String
let age: Int
}
2. Car ๊ตฌ์กฐ์ฒด๋ฅผ JSON ์ผ๋ก ์ธ์ฝ๋ฉํด๋ณด์
let data = Car(name: "taycan", brand: "porsche", age: 2)
let encoder = JSONEncoder() // 1๏ธโฃ JSON Encoder ์ ์ธ
let jsonData = try? encoder.encode(data) // 2๏ธโฃ Data ํ์
์ผ๋ก ๋ณํ
if let jsonData = jsonData, let jsonString = String(data: jsonData, encoding: .utf8) {
print("๐")
print(jsonString)
print("๐")
} // 3๏ธโฃ Data ํ์
์ String ์ผ๋ก ๋ง๋ฌ
// โ
if let ์ธ ์ด์
// 1. try? -> jsonData ๊ฐ optional ์ด๋ค.
// 2. 2. String(data: , encoding: )์ ๋ฆฌํดํ์
-> String? => optional
์ถ๋ ฅ๊ฒฐ๊ณผ๋ ์๋์ ๊ฐ๋ค
๐
{"name":"taycan","brand":"porsche","age":2}
๐
๊ทผ๋ฐ json ์ ์ฝ๊ฐ ๊ทธ ๋ชจ์
์ด ๊ตญ๋ฃฐ์ธ๋ฐ....?
๊ทธ ๋ชจ์
์ ์๋์์ ๋ง๋ค์ด๋ณด์
3. encoder์ ์ถ๋ ฅ๊ฒฐ๊ณผ๋ฅผ ์ด์๊ฒ ๋ง๋ค์ด๋ณด์ ๐ (JSONEncoder.OutputFormatting)
JSONEncoder.OutputFormatting ์ ์ฌ์ฉํ๋ฉด ๋๋ค!
encoder.outputFormatting = .prettyPrinted // json ๋ชจ์์ผ๋ก ์ด์๊ฒ ์ถ๋ ฅ
encoder.outputFormatting = .sortedKeys // key ๋ฅผ abcd ์์ผ๋ก ์ถ๋ ฅ
encoder.outputFormatting = .withoutEscapingSlashes // *escaping slash๋ฅผ ์ ๊ฑฐ
- escaping slash๋ฅผ ์ ๊ฑฐ
url๊ฐ์ "https://google.com" ์ผ๋ก ๋ณด๋๋๋ฐ, ์ค์ json ํ์ผ์์ "https:\/\/google.com"๋ก ๋๋ ๊ฒ์ ๋ฐฉ์งํ ์ ์์
์ฌ๋ฌ๊ฐ๋ฅผ ํ๋ฒ์ ์ ์ฉ
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
์ง !
์ด๋ฒ์ ๋ฐ๋๋ก Decoding ์ ํด๋ด ์๋ค~!~~
1. Codable ํ๋กํ ์ฝ์ ์ฑํํ Car ๊ตฌ์กฐ์ฒด๊ฐ ์๋ค
struct Car: Codable {
let name: String
let brand: String
let age: Int
}
2. Car ๊ตฌ์กฐ์ฒด๋ฅผ JSON ์ผ๋ก ๋์ฝ๋ฉํด๋ณด์
// 1๏ธโฃ JSONDecoder ์ ์ธ
let decoder = JSONDecoder()
// 2๏ธโฃ ์์๋ก JSON Data๋ฅผ ๋ง๋ค์ด๋ด
์๋ค (์์์ ๋ด์ฉ ์ฌ์ฉ)
let data = jsonString.data(using: .utf8)
// 3๏ธโฃ .decode ๋ฉ์๋๋ก data -> ์ธ์คํด์ค
if let data = data, let myCar = try? decoder.decode(Car.self, from: data) {
print("๐๐๐๐")
print(myCar.name)
print(myCar.brand)
print(myCar.age)
print("๐๐๐๐")
}
์ถ๋ ฅ๊ฒฐ๊ณผ
๐๐๐๐
taycan
porsche
2
๐๐๐๐
JSONDecoder ์ .decode
๋ฉ์๋๋ฅผ ๋ด
์๋ค
ํน์ ํ๋์ ๊ฐ์ด nullable ํ๊ฑฐ๋ optional ํ key์ ๊ฒฝ์ฐ struct์ ์๋์ ๊ฐ์ด ์ ์ํ ์ ์๋ค!
struct Car: Codable {
let name: String
let brand: String
let age: Int?
}
๊ธฐ๋ณธ๊ฐ ํ ๋น์ ์๋์ฒ๋ผ
struct Car: Codable {
let name: String
let brand: String
let age: Int = 0
}
https://zeddios.tistory.com/373
https://inuplace.tistory.com/895?category=1034357
https://medium.com/humanscape-tech/swift%EC%97%90%EC%84%9C-codable-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0-367587c5a591
https://medium.com/doyeona/codable-decodable-encodable-feat-json-5643dc3d7766
http://minsone.github.io/programming/swift-codable-and-exceptions-extension
https://kyungmosung.github.io/2020/08/17/swift-codable-enum/
https://developer.apple.com/documentation/swift/codable
https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types