Last active
March 4, 2024 01:08
-
-
Save tigi44/66f4d6ac7bf91a8fe1b85559eaf6ba2a to your computer and use it in GitHub Desktop.
iOS16이하 버전에서 한글이 포함된 URL을 사용할 때 Decodable URL 이슈 발생 (iOS17 부터는 한글 이슈 없음), iOS16이하에서는 URL 대신 EncodingURL로 사용
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* { | |
* "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