처음 스위프트 4에서 Codable
프로토콜을 사용하고 있는데 의 사용을 Decodable
에서 이해할 수 없습니다.JSON 구문 분석 용 Decodable을 사용할 때 선택 사항과 decodeIfPresent의 차이점은 무엇입니까?
/// Decodes a value of the given type for the given key, if present.
///
/// This method returns `nil` if the container does not have a value associated with `key`, or if the value is null. The difference between these states can be distinguished with a `contains(_:)` call.
///
/// - parameter type: The type of value to decode.
/// - parameter key: The key that the decoded value is associated with.
/// - returns: A decoded value of the requested type, or `nil` if the `Decoder` does not have an entry associated with the given key, or if the value is a null value.
/// - throws: `DecodingError.typeMismatch` if the encountered encoded value is not convertible to the requested type.
public func decodeIfPresent(_ type: String.Type, forKey key: KeyedDecodingContainer.Key) throws -> String?
값이 연관된 키와 함께 표시되지 않으면 nil
을 반환하는 것이 좋습니다. 이것이 유일한 이유 일 경우 선택적 속성과 값이 다른 점은 선택적 변수로 값이 응답에없는 경우 nil
으로 설정됩니다.
이 메소드는'String? '을 반환한다는 것을 알고 있습니까? 이것은 선택 사항입니까? 그래서 기본적으로 당신이 옳습니다. 거기에 optionals의 사용 _is_입니다. – Sweeper