With my newbie swift skills I'm struggling to figure out the correct swift syntax to get this playground to work. Depending how I try to solve it I either get
Cannot invoke 'encode' with an argument list of type '(Encodable)'
which is similar to what was solved in this question Using JSON Encoder to encode a variable with Codable as type or I get
' (T) -> ()' requires that 'Encodable' conform to 'Encodable'
I'd really appreciate a solution with explanation
EDIT To provide some more context, the pattern I'm trying to implement here is for a middleware router. Depending on the actions made in the app, the router will construct the network request. The intent of the codableParam is to deliver the conforming structure for the use case. All cases would therefore return either nil or a Codable type.
struct unityAuthenticationRequest: Codable {
var username : String
var password : String
}
enum test {
case volume
case num2
case num3
var codableParam: Encodable? {
switch self {
case .volume:
return unityAuthenticationRequest(username: "uname", password: "pwrods")
default:
return nil
}
}
}
func saveObject<T:Encodable>(_ object: T) {
let data = try? JSONEncoder().encode(object)
}
func dx<T: Codable>(fx: T) {
let datax = try? JSONEncoder().encode(fx)
}
let r = test.volume
saveObject(r.codableParam)