Skip to content

Commit beb4ca7

Browse files
authored
Add Conformance of 'DateInterval' to protocol 'Decodable'
Add Conformance of 'DateInterval' to protocol 'Decodable' to match Darwin.
1 parent d6f7085 commit beb4ca7

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
@@ -218,3 +218,23 @@ extension DateInterval : _ObjectTypeBridgeable {
218218
return result!
219219
}
220220
}
221+
222+
extension DateInterval : Codable {
223+
enum CodingKeys: String, CodingKey {
224+
case start
225+
case duration
226+
}
227+
228+
public init(from decoder: Decoder) throws {
229+
let container = try decoder.container(keyedBy: CodingKeys.self)
230+
let start = try container.decode(Date.self, forKey: .start)
231+
let duration = try container.decode(TimeInterval.self, forKey: .duration)
232+
self.init(start: start, duration: duration)
233+
}
234+
235+
public func encode(to encoder: Encoder) throws {
236+
var container = encoder.container(keyedBy: CodingKeys.self)
237+
try container.encode(self.start, forKey: .start)
238+
try container.encode(self.duration, forKey: .duration)
239+
}
240+
}

0 commit comments

Comments
 (0)