matthewcheok / JSONCodable

Hassle-free JSON encoding and decoding in Swift
MIT License
602 stars 66 forks source link

Problem JSON encoding and decoding in Swift with JSONCodable #79

Open CPiersigilli opened 7 years ago

CPiersigilli commented 7 years ago

I have this code for os x simple app, but it does not work:

import Cocoa

struct GeoFolderData {
    var nomeCartella:String
    var nomeCommittente:String?
    var dataCreazioneCartella:NSDate
    var dataModificaCartella:NSDate
    var isActive:Bool
}

extension GeoFolderData: JSONDecodable {
    init(object: JSONObject) throws {
        let decoder = JSONDecoder(object: object)
        nomeCartella = try decoder.decode("Nome_Cartella")
        nomeCommittente = try decoder.decode("Nome_Committente")
        dataCreazioneCartella = try decoder.decode(key: "dataCreazioneCartella")
        dataModificaCartella = try decoder.decode(key: "dataModificaCartella")
        isActive = try decoder.decode("Is_Active")
    }
}

extension GeoFolderData: JSONEncodable {
    func toJSON() throws -> Any {
        return try JSONEncoder.create({ (encoder) -> Void in
            try encoder.encode(nomeCartella, key: "Nome_Cartella")
            try encoder.encode(nomeCommittente, key: "Nome_Committente")
            try encoder.encode(dataCreazioneCartella, key: "Data_Creazione_Cartella")
            try encoder.encode(dataModificaCartella, key: "Data_Modifica_Cartella")
            try encoder.encode(isActive, key: "Is_Active")
        })
    }
}

extension JSONDecoder {
    public func decode(key: String) throws -> NSDate {
        return try decode(String, transformer: JSONTransformer<StringToNSDate, StringToNSDate>)
    }
}

With these errors: screenshot 2017-04-24 23 24 56

Is there anyone who can help me? Thank you.

Mozahler commented 7 years ago

Did you trying asking this question on stackoverflow.com?

CPiersigilli commented 7 years ago

No. Now, see JSON encoding and decoding with “matthewcheok/JSONCodable” on stackoverflow.com

pronebird commented 6 years ago

You don't seem to import JSONCodable.