JSONDecoder

YesCoach·2021년 7월 6일
0

JSONDecoder 공식문서

JSON 개체에서 데이터 유형의 인스턴스를 디코딩하는 객체

Declaration

class JSONDecoder

Overview

아래 예시에서는 JSON 객체에서 단순 GroceryProduct 타입의 인스턴스를 디코딩하는 방법을 보여 줍니다. 이 유형은 Codable을 채택하여 JSONDecoder 인스턴스를 사용하여 디코딩할 수 있습니다.

struct GroceryProduct: Codable {
    var name: String
    var points: Int
    var description: String?
}

let json = """
{
    "name": "Durian",
    "points": 600,
    "description": "A fruit with a distinctive scent."
}
""".data(using: .utf8)!

let decoder = JSONDecoder()
let product = try decoder.decode(GroceryProduct.self, from: json)

print(product.name) // Prints "Durian"

Topics


First Steps

  • init()
    기본 포맷 설정과 디코딩 전략을 사용하여 재사용 가능한 새 JSON 디코더를 만듭니다.
  • func decode<T>(T.Type, from: Data) -> T
    지정한 유형의 값을 JSON 개체에서 디코딩하여 반환합니다.

Customizing Decoding

  • var keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy
    타입의 코딩 키를 JSON 키로 디코딩하는 방법을 결정하는 값.

  • enum JSONDecoder.KeyDecodingStrategy
    타입의 코딩 키를 JSON 키로 디코딩하는 방법을 결정하는 값.

  • var userInfo: [CodingUserInfoKey : Any]
    상황에 맞는 정보를 제공하여 디코딩 프로세스를 커스터마이징 하는 데 사용하는 딕셔너리입니다.

Decoding Dates

Decoding Raw Data

Decoding Exceptional Numbers

Type Aliases

  • typealias JSONDecoder.Input

Instance Properties

  • var allowsJSON5: Bool
  • var assumesTopLevelDictionary: Bool

See Also

JSON

profile
iOS dev / Japanese with Computer Science

0개의 댓글