0

Creating a data model to use with codable and realm. Im getting stuck on dictionaries. As I understand it, you cannot use dictionaries with realm? How can I decode the following JSON and use it with something Realm will accept?

JSON Example A

"platform": {
            "data": {
                "25": {
                    "id": 25,
                    "name": "3DO",
                    "alias": "3do"
                },
                "4944": {
                    "id": 4944,
                    "name": "Acorn Archimedes",
                    "alias": "acorn-archimedes"
                },
                "4954": {
                    "id": 4954,
                    "name": "Acorn Electron",
                    "alias": "acorn-electron"
                }
}

Note, the keys are strings that change, in decoable form i have it as

StructExample A

struct PlatformData : Codable {

let data : [String: PlatformInformation]
}

JSON Example B

"include": {
        "boxart": {
            "base_url": {
                "original": "https:\/\/cdn.thegamesdb.net\/images\/original\/",
                "small": "https:\/\/cdn.thegamesdb.net\/images\/small\/",
                "thumb": "https:\/\/cdn.thegamesdb.net\/images\/thumb\/",
                "cropped_center_thumb": "https:\/\/cdn.thegamesdb.net\/images\/cropped_center_thumb\/",
                "medium": "https:\/\/cdn.thegamesdb.net\/images\/medium\/",
                "large": "https:\/\/cdn.thegamesdb.net\/images\/large\/"
            },
            "data": {
                "1": 
                [
                    {
                        "id": 242,
                        "type": "fanart",
                        "side": null,
                        "filename": "fanart\/1-2.jpg",
                        "resolution": "1920x1080"
                    },
                    {
                        "id": 433,
                        "type": "fanart",
                        "side": null,
                        "filename": "fanart\/1-5.jpg",
                        "resolution": "1920x1080"
                    }
],
                "2": 
                [
                    {
                        "id": 15,
                        "type": "fanart",
                        "side": null,
                        "filename": "fanart\/2-1.jpg",
                        "resolution": "1920x1080"
                    },
                    {
                        "id": 133,
                        "type": "fanart",
                        "side": null,
                        "filename": "fanart\/2-2.jpg",
                        "resolution": "1920x1080"
                    },

                    {
                        "id": 656,
                        "type": "fanart",
                        "side": null,
                        "filename": "fanart\/2-4.jpg",
                        "resolution": "1920x1080"
                    }

                ],
                "4": 
                [
                    {
                        "id": 208,
                        "type": "fanart",
                        "side": null,
                        "filename": "fanart\/4-1.jpg",
                        "resolution": "1920x1080"
                    },
                    {
                        "id": 481,
                        "type": "banner",
                        "side": null,
                        "filename": "graphical\/4-g.jpg",
                        "resolution": null
                    },
                    {
                        "id": 846,
                        "type": "boxart",
                        "side": "front",
                        "filename": "boxart\/front\/4-1.jpg",
                        "resolution": "1000x705"
                    },
                    {
                        "id": 847,
                        "type": "boxart",
                        "side": "back",
                        "filename": "boxart\/back\/4-1.jpg",
                        "resolution": "1000x705"
                    },
                    {
                        "id": 73897,
                        "type": "clearlogo",
                        "side": null,
                        "filename": "clearlogo\/4.png",
                        "resolution": "400x148"
                    },
                    {
                        "id": 215539,
                        "type": "fanart",
                        "side": null,
                        "filename": "fanart\/4-2.jpg",
                        "resolution": null
                    },
                    {
                        "id": 238533,
                        "type": "screenshot",
                        "side": null,
                        "filename": "screenshot\/4-1.jpg",
                        "resolution": null
                    },
                    {
                        "id": 238534,
                        "type": "screenshot",
                        "side": null,
                        "filename": "screenshot\/4-2.jpg",
                        "resolution": null
                    },
                    {
                        "id": 238535,
                        "type": "screenshot",
                        "side": null,
                        "filename": "screenshot\/4-3.jpg",
                        "resolution": null
                    },
                    {
                        "id": 238536,
                        "type": "screenshot",
                        "side": null,
                        "filename": "screenshot\/4-4.jpg",
                        "resolution": null
                    },
                    {
                        "id": 238537,
                        "type": "screenshot",
                        "side": null,
                        "filename": "screenshot\/4-5.jpg",
                        "resolution": null
                    },
                    {
                        "id": 238538,
                        "type": "screenshot",
                        "side": null,
                        "filename": "screenshot\/4-6.jpg",
                        "resolution": null
                    }
                ],
                "5": 
                [
                    {
                        "id": 396,
                        "type": "banner",
                        "side": null,
                        "filename": "graphical\/5-g.jpg",
                        "resolution": null
                    },
                    {
                        "id": 2364,
                        "type": "fanart",
                        "side": null,
                        "filename": "fanart\/5-4.jpg",
                        "resolution": "1920x1080"
                    },
]
                

Here is similar, except now with an array of objects as the value Struct Example B

struct BoxArtData : Codeable {
let data: [String:[ExtraImages]]
}
  • RealmSwift doesn't support dictionaries. Take a look at [this answer](https://stackoverflow.com/a/33801227/7245977) – DoesData Sep 22 '20 at 00:58
  • 1
    @DoesData That answer is from 2015 and is pretty well outdated. You can easily [create objects from dictionaries](https://realm.io/docs/swift/latest/#creating-objects) to be used with Realm. You can even use [Nested Dicts](https://realm.io/docs/swift/latest/#nested-objects) to create objects. Encode and Decode JSON is supported as well. But: an answer will depend on how the objects interact. In other words, in example A, *platform* has *data* but that child data seems to be a List in itself of other objects. Is that correct? – Jay Sep 22 '20 at 18:50
  • @Jay that is correct, in the dictionary it has an ID number as a string as the key, and then a list of other objects as the value. If i feed in the appropriate ID string, I can access the underlying objects. – thesuffering Sep 22 '20 at 19:49
  • Assuming you know how to read that json, you can use the data to initialize a realm object; `let realmObject = RealmObjectClass(value: ["id" : 242, "type": "fanart"])` and then add it to a list `let myList = List`. Lists are very similar to array and behave in similar ways. – Jay Sep 24 '20 at 18:42

0 Answers0