Pircate / CleanJSON

Swift JSON decoder for Codable
MIT License
292 stars 40 forks source link

使用class时,子类Model赋值失败 #6

Closed NuGelaLies closed 5 years ago

NuGelaLies commented 5 years ago
class father: Codable {
    var id: String
    var newsID: String
    var title: String
    var content: String
    var section: String
    var cat: String
    var publishDatetime: String
    var layout: String

    private enum CodingKeys: String, CodingKey {
        case id
        case newsID
        case title
        case content
        case section
        case cat
        case publishDatetime = "publish_datetime"
        case layout
    }
}

class son: father {

    var landingType: String
    var landingUrl: String

    var cover: String
    var pos: String
    var listPos: String
    var image: String

    private enum CodingKeys: String, CodingKey {
        case landingType = "landing_type"
        case landingUrl = "landing_url"
        case cover
        case pos
        case listPos = "list_pos"
        case image
    }
}
json 

{"data":[{"id":"49","landing_type":"2","landing_url":"http:\/\/hlapp.stheadline.com\/ads_view\/49","title":"test inhouse","content":null,"cover":"49","pos":"2","list_pos":"4","image":null,"layout":"3"},{"id":"47","landing_type":"1","landing_url":"http:\/\/192.168.148.244\/headline_app_back_end\/Ads\/form","title":null,"content":null,"cover":"47","pos":"1","list_pos":"2","image":"hl_app_ads\/2019\/05\/22\/20190522104030_9444.jpg","layout":"1"}],"result":1}

子类中除了父类包含的属性,其他属性都没法赋值

Pircate commented 5 years ago

https://bignerdcoding.com/archives/44.html

建议看一下这篇文章,有介绍继承的用法。

NuGelaLies notifications@github.com于2019年6月30日 周日18:27写道:

class father: Codable {

var id: String

var newsID: String

var title: String

var content: String

var section: String

var cat: String

var publishDatetime: String

var layout: String

private enum CodingKeys: String, CodingKey {

    case id

    case newsID

    case title

    case content

    case section

    case cat

    case publishDatetime = "publish_datetime"

    case layout

}

}

class son: father {

var landingType: String

var landingUrl: String

var cover: String

var pos: String

var listPos: String

var image: String

private enum CodingKeys: String, CodingKey {

    case landingType = "landing_type"

    case landingUrl = "landing_url"

    case cover

    case pos

    case listPos = "list_pos"

    case image

}

}

json

{"data":[{"id":"49","landing_type":"2","landing_url":"http:\/\/hlapp.stheadline.com\/ads_view\/49","title":"test inhouse","content":null,"cover":"49","pos":"2","list_pos":"4","image":null,"layout":"3"},{"id":"47","landing_type":"1","landing_url":"http:\/\/192.168.148.244\/headline_app_back_end\/Ads\/form","title":null,"content":null,"cover":"47","pos":"1","list_pos":"2","image":"hl_app_ads\/2019\/05\/22\/20190522104030_9444.jpg","layout":"1"}],"result":1}

子类中除了父类包含的属性,其他属性都没法赋值

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Pircate/CleanJSON/issues/6?email_source=notifications&email_token=ADY7OITP5G3632NH2ILVBSTP5CDCTA5CNFSM4H4MIHAKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4G4PQTVA, or mute the thread https://github.com/notifications/unsubscribe-auth/ADY7OIQEFKNJMRB5B2LXHRTP5CDCTANCNFSM4H4MIHAA .

qsfty commented 3 years ago

亲,文档失效了,可否在此回复下?