将Swift Codable类迁移到领域

时间:2019-04-07 01:46:26

标签: json swift realm

所以我有一些使用alamofire从api中提取的JSON数据。当数据返回时,结构看起来像这样

   {
      "sneakers" : {
        "brands" : {
          "Adidas" : [ {
            "brand" : "adidas",
            "category" : "lifestyle",
            "colorway" : "Cream White/Cream White/Core White",
            "description" : "First released on April 29, 2017, the Yeezy Boost 350 V2 ‘Cream White’ combines a cream Primeknit upper with tonal cream SPLY 350 branding, and a translucent white midsole housing full-length Boost. Released again in October 2018, this retro helped fulfill Kanye West’s oft-repeated ‘YEEZYs for everyone’ Twitter mantra, as adidas organized the biggest drop in Yeezy history by promising pre-sale to anyone who signed up on the website. Similar to the first release, the ‘Triple White’ 2018 model features a Primeknit upper, a Boost midsole and custom adidas and Yeezy co-branding on the insole.",
            "designer" : "Kanye West",
            "imagesrc" : "https://image.goat.com/crop/1250/attachments/product_template_additional_pictures/images/014/822/695/original/116662_03.jpg.jpeg",
            "maincolor" : "White",
            "name" : "Yeezy Boost 350 V2 'Cream White / Triple White'",
            "nickname" : "Cream White / Triple White",
            "price" : "Buy New - $220",
            "productlink" : "$190YEEZY BOOST 350 V2 'CREAM WHITE / TRIPLE WHITE'",
            "productlinkhref" : "https://www.goat.com/sneakers/yeezy-boost-350-v2-cream-white-cp9366",
            "releasedate" : "2017-04-29",
            "silhouette" : "Yeezy Boost 350",
            "technology" : "Boost",
            "webscraperorder" : "1554084922-147",
            "webscraperstarturl" : "https://www.goat.com/sneakers"
          }
]
}
}
}

在可能存在更多品牌(耐克,Air Jordan)的情况下,这些鞋下带有更多运动鞋。使用Swift Decodable协议,我能够将JSON数据正确解析为易于为任何用户读取的对象。可以在下面看到此代码。

import Foundation
import RealmSwift
import Realm


struct Sneaker: Decodable {
    var sneakers : Sneakers

}

struct Sneakers: Decodable {
    let brands: Shoe
}



struct Shoe: Decodable {
    let adidas: [SneakerInfo]
    let nike: [SneakerInfo]
    let airjordan: [SneakerInfo]
    let vans: [SneakerInfo]
    enum CodingKeys: String, CodingKey {
        case adidas = "Adidas"
        case nike = "Nike"
        case airjordan = "Air Jordan"
        case vans = "Vans"
    }
}


struct SneakerInfo: Decodable {
    let brand, category, colorway, description: String?
    let designer: String?
    let imagesrc: String?
    let maincolor, name, nickname, price: String?
    let productlink: String?
    let productlinkhref: String?
    let releasedate, silhouette, technology, webscraperorder: String?
    let webscraperstarturl: String?
    enum CodingKeys: String, CodingKey {
        case brand, category, colorway, description, designer, imagesrc, maincolor, name, nickname, price, productlink, productlinkhref, releasedate, silhouette, technology,webscraperorder,webscraperstarturl
    }
}

目前,我正在尝试迁移相同的代码,以准备与领域和可编码一起使用,但我一直遇到错误。

我的问题是抱怨我的实现和初始化。 我将在下面包括课程。我整天都被困住了。

import Foundation
import Realm
import RealmSwift

@objcMembers class SneakerTemp: Object, Decodable {
    dynamic var sneaker: SneakersTemp

    enum CodingKeys: String, CodingKey {
        case sneaker
    }

    required init()
    {
        super.init()
    }

 override static func primaryKey() -> String?
    {
        return  UUID().uuidString
    }

    required init(from decoder: Decoder) throws
    {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        sneaker = try container.decode(SneakersTemp.self, forKey: .sneaker)

        super.init()
    }

    required init(value: Any, schema: RLMSchema)
    {
        super.init(value: value, schema: schema)
    }

    required init(realm: RLMRealm, schema: RLMObjectSchema)
    {
        super.init(realm: realm, schema: schema)
    }
}

@objcMembers class SneakersTemp: Object, Decodable {
    dynamic var brands:ShoeTemp
    enum CodingKeys: String, CodingKey {
        case brands
    }

    required init()
    {
        super.init()
    }
 override static func primaryKey() -> String?
{
    return  UUID().uuidString
}

    required init(from decoder: Decoder) throws
    {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        brands = try container.decode(ShoeTemp.self, forKey: .brands)

        super.init()
    }

    required init(value: Any, schema: RLMSchema)
    {
        super.init(value: value, schema: schema)
    }

    required init(realm: RLMRealm, schema: RLMObjectSchema)
    {
        super.init(realm: realm, schema: schema)
    }
}

@objcMembers class ShoeTemp: Object, Decodable {
        let adidas = RealmSwift.List<SneakerInfoTemp>()
        let nike = RealmSwift.List<SneakerInfoTemp>()
        let airjordan = RealmSwift.List<SneakerInfoTemp>()
        let vans = RealmSwift.List<SneakerInfoTemp>()

        enum CodingKeys: String, CodingKey {
            case adidas = "Adidas"
            case nike = "Nike"
            case airjordan = "Air Jordan"
            case vans = "Vans"
        }

        required init(from decoder: Decoder) throws
        {
            let container = try decoder.container(keyedBy: CodingKeys.self)

            adidas = try container.decode(String.self, forKey: .adidas)
            nike = try container.decode(String.self, forKey: .nike)
            airjordan = try container.decode(String.self, forKey: .airjordan)
            vans = try container.decode(String.self, forKey: .vans)

            super.init()
        }

        required init()
        {
            super.init()
        }



override static func primaryKey() -> String?
    {
        return  UUID().uuidString
    }

        required init(value: Any, schema: RLMSchema)
        {
            super.init(value: value, schema: schema)
        }

        required init(realm: RLMRealm, schema: RLMObjectSchema)
        {
            super.init(realm: realm, schema: schema)
        }
    }

 @objcMembers class SneakerInfoTemp: Object, Decodable {
        dynamic var brand: String?
        dynamic var category: String?
        dynamic var colorway: String?
        dynamic var descriptionTemp: String?
        dynamic var designer: String?
        dynamic var imagesrc: String?
        dynamic var maincolor: String?
        dynamic var name: String?
        dynamic var nickname: String?
        dynamic var price: String?
        dynamic var productlink: String?
        dynamic var productlinkhref: String?
        dynamic var releasedate: String?
        dynamic var silhouette: String?
        dynamic var technology: String?
        dynamic var webscraperorder: String?
        dynamic var webscraperstarturl: String?

        enum CodingKeys: String, CodingKey {
            case brand, category, colorway, designer, imagesrc, maincolor, name, nickname, price, productlink, productlinkhref, releasedate, silhouette, technology,webscraperorder,webscraperstarturl
            case descriptionTemp = "description"
        }

        required init(from decoder: Decoder) throws
        {
            let container = try decoder.container(keyedBy: CodingKeys.self)

            brand = try container.decode(String.self, forKey: .brand)
            category = try container.decode(String.self, forKey: .category)
            colorway = try container.decode(String.self, forKey: .colorway)
            descriptionTemp = try container.decode(String.self, forKey: .descriptionTemp)
            designer = try container.decode(String.self, forKey: .designer)
            imagesrc = try container.decode(String.self, forKey: .imagesrc)
            maincolor = try container.decode(String.self, forKey: .maincolor)
            name = try container.decode(String.self, forKey: .name)
            nickname = try container.decode(String.self, forKey: .nickname)

            price = try container.decode(String.self, forKey: .price)
            productlink = try container.decode(String.self, forKey: .productlink)
            productlinkhref = try container.decode(String.self, forKey: .productlinkhref)
            releasedate = try container.decode(String.self, forKey: .releasedate)
            silhouette = try container.decode(String.self, forKey: .silhouette)
            technology = try container.decode(String.self, forKey: .technology)
            webscraperorder = try container.decode(String.self, forKey: .webscraperorder)
            webscraperstarturl = try container.decode(String.self, forKey: .webscraperstarturl)
            super.init()
        }


        required init()
        {
            super.init()
        }

        required init(value: Any, schema: RLMSchema)
        {
            super.init(value: value, schema: schema)
        }

        required init(realm: RLMRealm, schema: RLMObjectSchema)
        {
            super.init(realm: realm, schema: schema)
        }


    }

主要错误是

  

未在super.init调用中初始化属性“ self.sneaker”

  

未在super.init调用中初始化属性“ self.brands”

1 个答案:

答案 0 :(得分:0)

在一个类中,sneakerbrands需要初始化或可选,以将它们初始化为nil。结构具有默认的成员初始化器。

import Foundation
import Realm
import RealmSwift

class SneakerTemp: Object, Decodable {
    @objc dynamic var id = UUID().uuidString
    @objc dynamic var sneaker = SneakersTemp()
    // or @objc dynamic var sneaker: SneakersTemp?
    override static func primaryKey() -> String? {
      return "id"
    }
}

class SneakersTemp: Object, Decodable {
    @objc dynamic var id = UUID().uuidString
    @objc dynamic var brands = ShoeTemp()
    // or @objc dynamic var brands: ShoeTemp?
    override static func primaryKey() -> String? {
      return "id"
    }
}

class ShoeTemp: Object, Decodable {
    @objc dynamic var id = UUID().uuidString
    let adidas = List<SneakerInfoTemp>()
    let nike = List<SneakerInfoTemp>()
    let airjordan = List<SneakerInfoTemp>()
    let vans = List<SneakerInfoTemp>()
    override static func primaryKey() -> String? {
      return "id"
    }

    enum CodingKeys: String, CodingKey {
        case adidas = "Adidas"
        case nike = "Nike"
        case airjordan = "Air Jordan"
        case vans = "Vans"
    }
}

class SneakerInfoTemp: Object, Decodable {
    @objc dynamic var brand: String?
    @objc dynamic var category: String?
    @objc dynamic var colorway: String?
    @objc dynamic var descriptionTemp: String?
    @objc dynamic var designer: String?
    @objc dynamic var imagesrc: String?
    @objc dynamic var maincolor: String?
    @objc dynamic var name: String?
    @objc dynamic var nickname: String?
    @objc dynamic var price: String?
    @objc dynamic var productlink: String?
    @objc dynamic var productlinkhref: String?
    @objc dynamic var releasedate: String?
    @objc dynamic var silhouette: String?
    @objc dynamic var technology: String?
    @objc dynamic var webscraperorder: String?
    @objc dynamic var webscraperstarturl: String?

    enum CodingKeys: String, CodingKey {
        case brand, category, colorway, designer, imagesrc, maincolor, name, nickname, price, productlink, productlinkhref, releasedate, silhouette, technology,webscraperorder,webscraperstarturl
        case descriptionTemp = "description"
    }
}