DateEncodingStrategy.custom encoder is immutable type?

Trying to implement a custom date encoding strategy using the following leads to a compile time error (smallest possible example to create error, not my prod code here)

let encoder = JSONEncoder()

encoder.dateEncodingStrategy = .custom({ date, encoder in
	encoder.singleValueContainer().encode(date.timeIntervalSince1970)
})

This produces the following error (which makes sense as it is not an inout variable)

Cannot use mutating member on immutable value: function call returns immutable value

Am I using the API incorrectly? If so what is the correct way to encode the data?

Answered by neilfrommartock in 699328022

Answer my own question here, it was a silly mistake from me, the error was about the container not the encoder, correct code looks as follows

let encoder = JSONEncoder()

encoder.dateEncodingStrategy = .custom({ date, encoder in
	var container = encoder.singleValueContainer()
  try container.encode(date.timeIntervalSince1970)
})

It's been a long year!!!!

Accepted Answer

Answer my own question here, it was a silly mistake from me, the error was about the container not the encoder, correct code looks as follows

let encoder = JSONEncoder()

encoder.dateEncodingStrategy = .custom({ date, encoder in
	var container = encoder.singleValueContainer()
  try container.encode(date.timeIntervalSince1970)
})

It's been a long year!!!!

(Duplicate answer removed)

DateEncodingStrategy.custom encoder is immutable type?
 
 
Q