utahiosmac / Marshal

Marshaling the typeless wild west of [String: Any]
MIT License
697 stars 62 forks source link

Marshaling Date? #139

Open JetForMe opened 5 years ago

JetForMe commented 5 years ago

So, it's clear to me how to unmarshal a Date, by extending it with the ValueType protocol. But it's not at all clear how to marshal it. I tried various forms of this:

extension
Date : ValueType, Marshaling
{
    public
    static
    func
    value(from inObj: Any)
        throws
        -> Date
    {
        guard
            let timeInterval = inObj as? Double
        else
        {
            throw MarshalError.typeMismatch(expected: Double.self, actual: type(of: inObj))
        }

        let date = Date(timeIntervalSinceReferenceDate: timeInterval)
        return date
    }

    public
    func
    marshaled()
        -> Double
    {
        return self.timeIntervalSinceReferenceDate
    }
}

But Xcode doesn't like it. In this case, it says Type 'Date' does not conform to protocol 'Marshaling'. Not sure what I'm doing wrong.