20 lines
321 B
Swift
20 lines
321 B
Swift
import Foundation
|
|
|
|
extension Decodable {
|
|
init?(jsonData: Data) {
|
|
guard let value = try? JSONDecoder().decode(Self.self, from: jsonData) else {
|
|
return nil
|
|
}
|
|
|
|
self = value
|
|
}
|
|
|
|
init?(jsonString: String) {
|
|
guard let data = jsonString.data(using: .utf8) else {
|
|
return nil
|
|
}
|
|
|
|
self.init(jsonData: data)
|
|
}
|
|
}
|