Swift Codable:如何从嵌入数据中创建多个对象

时间:2018-05-06 14:07:48

标签: swift codable

说我有这样的JSON:

    ["conferences": <__NSArrayI 0x60c00002f720>(
    {
        alias = Conference1;
        divisions =     (
                    {
                alias = "Division 1";
                id = "b95cd27d-d631-4fe1-bc05-0ae47fc0b14b";
                name = "Division 1";
                teams =             (
                                    {
                        alias = OXC;
                        id = "768c92aa-75ff-4a43-bcc0-f2798c2e1724";
                        market = East;
                        name = Rams;
                        references =                     (
                                                    {
                                id = Rams;
                                origin = gsis;
                            }
                        );

                    {
                        alias = AXC;
                        id = "768c92aa-75ff-4a43-bcc0-f2798c2e1724";
                        market = East;
                        name = Platypus;
                        references =                     (
                                                    {
                                id = Platypus;
                                origin = gsis;
                            }
                        );

                    },
{
           alias = "Division 2";
                id = "b95cd27d-d631-4fe1-bc05-0ae47fc0b14b";
                name = "Division 2";
                teams =             (
                                    {
                        alias = NXC;
                        id = "768c92aa-75ff-4a43-bcc0-f2798c2e1724";
                        market = West;
                        name = Ants;
                        references =                     (
                                                    {
                                id = Ants;
                                origin = gsis;
                            }
                        );

    {
                        alias = QXC;
                        id = "768c92aa-75ff-4a43-bcc0-f2798c2e1724";
                        market = West;
                        name = Bulls;
                        references =                     (
                                                    {
                                id = Bulls;
                                origin = gsis;
                            }
                        );

                    },
}]

我对如何创建包含团队数据的对象以及会议和分区别名感到很遗憾。

我开始沿着这条路走下去,但这似乎只是基本上将所有json转储到对象上。我显然不希望这样的开销。

struct Team:Codable {

    var arrConference:[Conference]

    private enum CodingKeys: String, CodingKey {
        case arrConference = "conferences"
    }

    struct Conference:Codable {

        var conferenceName:String
        var conferenceID:String

        private enum CodingKeys: String, CodingKey {
            case conferenceName = "alias"
            case conferenceID = "id"
        }
    }

}

我想我是以旧的方式思考,但你怎么用可编码的方式列举数据?

0 个答案:

没有答案