1

Im now sitting here on a, as I first thought, very simple problem. I want to encode a object in Swift, which inheritance another Class.

Here the Classes:

class NetworkMessage: Codable {
    var version:Int?
}

class AddPlayerRequest: NetworkMessage {

    var playerName: String?
    var playerHashedPw: String?

    private enum CodingKeys: String, CodingKey {
        case playerName
        case playerHashedPw
    }

    override func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(playerName, forKey: .playerName)
        try container.encode(playerHashedPw, forKey: .playerHashedPw)

        try super.encode(to: encoder)
    }

}

I know there are millions of examples for that and I would say I am sure I did everything like in the examples, but it seems like I missed something, because when I try to encode a AddPlayerRequest like this:

let apr = AddPlayerRequest()
apr.playerName = "Test"
apr.playerHashedPw = "hynt87t7t76yt=="
apr.version = 1

do {
    let encoder = JSONEncoder()
    encoder.outputFormatting = .prettyPrinted
    let data = try encoder.encode(apr)
    let jsonStr = String(data: data, encoding: .utf8)
} catch {
    print(error)
}

The output is:

{

}

EDIT: It seems that the same Code is working on another MAC. Anyone has an idea why it is possiblöe that the Code is running on one MAC, but not on another?

julienduchow
  • 1,026
  • 1
  • 11
  • 29

0 Answers0