Skip to content

Instantly share code, notes, and snippets.

@tigi44
Last active March 4, 2024 01:08
Show Gist options
  • Save tigi44/66f4d6ac7bf91a8fe1b85559eaf6ba2a to your computer and use it in GitHub Desktop.
Save tigi44/66f4d6ac7bf91a8fe1b85559eaf6ba2a to your computer and use it in GitHub Desktop.
iOS16이하 버전에서 한글이 포함된 URL을 사용할 때 Decodable URL 이슈 발생 (iOS17 부터는 한글 이슈 없음), iOS16이하에서는 URL 대신 EncodingURL로 사용
public struct EncodingURL: Codable {
private let value: String
public var url: URL? {
guard let url = URL(string: value) else {
guard let encodedValue = value.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
let url = URL(string: encodedValue) else {
return nil
}
return url
}
return url
}
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
value = try container.decode(String.self)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(value)
}
}
/*
* {
* "url": "https://www.aaa.com/한글포함"
* }
* 위와 같은 json 데이터를 swift에서 decoding 하여 사용할때, iOS16이하에서 URL 대신 EncodingURL 로 사용
*/
struct AnyModel: Encodable {
let url: EncodingURL?
var encodingURL: URL? {
self.url?.url
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment