Skip to content

Commit e1c3411

Browse files
authored
Merge pull request #2426 from damuellen/decodable-patch-1
Add Conformance of 'DateInterval' to protocol 'Decodable'
2 parents de1042d + beb4ca7 commit e1c3411

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Foundation/DateInterval.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,23 @@ extension DateInterval : _ObjectiveCBridgeable {
213213
return result!
214214
}
215215
}
216+
217+
extension DateInterval : Codable {
218+
enum CodingKeys: String, CodingKey {
219+
case start
220+
case duration
221+
}
222+
223+
public init(from decoder: Decoder) throws {
224+
let container = try decoder.container(keyedBy: CodingKeys.self)
225+
let start = try container.decode(Date.self, forKey: .start)
226+
let duration = try container.decode(TimeInterval.self, forKey: .duration)
227+
self.init(start: start, duration: duration)
228+
}
229+
230+
public func encode(to encoder: Encoder) throws {
231+
var container = encoder.container(keyedBy: CodingKeys.self)
232+
try container.encode(self.start, forKey: .start)
233+
try container.encode(self.duration, forKey: .duration)
234+
}
235+
}

0 commit comments

Comments
 (0)