Difficulty: easy
You are given a JSON object with a single property date
containing a timestamp
in Epoch time.
{
"date": 459125100
}
Your task is to decode such an object into an instance of a type that conforms to the protocol
protocol P {
var date: Date { get }
}
The particular type you use is entirely arbitrary as long as it conforms to P
.
Write a method that takes a Data
instance representing a JSON object of the
aforementioned format and decodes it into an instance conforming to P
.
func decode(_ json: Data) throws -> P {
// ?
}
To start working on this challenge open Challenge.swift and uncomment the code skeleton.
To run unit tests that validate your code, uncomment the body of the test method
in ChallengeTests.swift and hit CMD + U
in Xcode. On Linux you can run the
tests by executing swift test
in the package directory.
To view selected solutions open Solutions.swift.