Как сохранить данные JSON в массиве классов моделей в Swift 5 - PullRequest
0 голосов
/ 01 октября 2019

Привет, я новичок в быстром ios, и мое требование - отобразить ответ Json на список таблиц. Я получил ответ от веб-служб, и ответ выглядит примерно так:

МОЕ требование - как сопоставить классы модели сМассив и как отобразить их в tableList, может кто-нибудь мне помочь, пожалуйста

МОЙ ОТВЕТ

[{
        "id": 6,
        "products": {
            "items": [{
                    "status": 1,
                    "custom_attributes": [

                        {
                            "attribute_code": "short_description",
                            "value": "short_description"
                        },
                        {
                            "attribute_code": "meta_title",
                            "value": "meta_title"
                        },
                        {
                            "attribute_code": "meta_keyword",
                            "value": "meta_keyword"
                        }
                    ]
                },
                {
                    "status": 1,
                    "custom_attributes": [

                        {
                            "attribute_code": "short_description",
                            "value": "short_description"
                        },
                        {
                            "attribute_code": "meta_title",
                            "value": "meta_title"
                        },
                        {
                            "attribute_code": "meta_keyword",
                            "value": "meta_keyword"
                        }
                    ]

                }
            ]
        }
    },
    {
        "id": 6,
        "products": {
            "items": [{
                    "status": 1,
                    "custom_attributes": [

                        {
                            "attribute_code": "short_description",
                            "value": "short_description"
                        },
                        {
                            "attribute_code": "meta_title",
                            "value": "meta_title"
                        },
                        {
                            "attribute_code": "meta_keyword",
                            "value": "meta_keyword"
                        }
                    ]
                },
                {
                    "status": 1,
                    "custom_attributes": [

                        {
                            "attribute_code": "short_description",
                            "value": "short_description"
                        },
                        {
                            "attribute_code": "meta_title",
                            "value": "meta_title"
                        },
                        {
                            "attribute_code": "meta_keyword",
                            "value": "meta_keyword"
                        }
                    ]
                }
            ]
        }
    }
]

Я пробовал это


class DashboardProduct: NSObject {

    class dashProductBook : NSObject {

        static var products : [Product]!
        //MARK: Init
        @discardableResult init(dictionary: [String:Any]){

            dashProductBook.products = [Product]()
            if let dashProductArray = dictionary["products"] as? [[String:Any]]{
                for dic in dashProductArray{
                    let value = Product(dictionary: dic)
                    dashProductBook.products.append(value)
                }
            }

        }

        //MARK: Delete
        static func deleteFromModel()
        {
            products = nil

        }

        //MARK: Return data
        static func toDictionary() -> [String:Any]
        {
            var dictionary = [String:Any]()
            if products != nil{
                var dictionaryElements = [[String:Any]]()
                for productElement in products {
                    dictionaryElements.append(productElement.toDictionary())
                }
                dictionary["products"] = dictionaryElements
            }
            return dictionary
        }
    }

    class Product : NSObject{


        var items : [Items]!

        init(dictionary: [String:Any]) {

            items = [Items]()
            if let regionArray = dictionary["items"] as? [[String:Any]]{
                for dic in regionArray{
                    let value = Items(dictionary: dic)
                    items.append(value)
                }
            }

        }

        func toDictionary() -> [String:Any]
        {
            var dictionary = [String:Any]()

            if items != nil{
                var dictionaryElements = [[String:Any]]()
                for itemElement in items {
                    dictionaryElements.append(itemElement.toDictionary())
                }
                dictionary["items"] = dictionaryElements
            }

            return dictionary
        }
    }


    class Items : NSObject{

        var custom_Attr : [Custom_Attribute]!


        init(dictionary: [String:Any]) {

            custom_Attr = [Custom_Attribute]()
            if let custom_attributesArray = dictionary["custom_attributes"] as? [[String:Any]]{
                for dic in custom_attributesArray{
                    let value = Custom_Attribute(dictionary: dic)
                    custom_Attr.append(value)
                }
            }

        }

        func toDictionary() -> [String:Any]
        {
            var dictionary = [String:Any]()

            if custom_Attr != nil{
                var dictionaryElements = [[String:Any]]()
                for customElement in custom_Attr {
                    dictionaryElements.append(customElement.toDictionary())
                }
                dictionary["custom_attributes"] = dictionaryElements
            }

            return dictionary
        }
    }


    class Custom_Attribute : NSObject{

        var key: String?
        var value: String?

        init(dictionary: [String: Any]) {
            self.key = dictionary["key"] as? String
            self.value = dictionary["value"] as? String
        }

        func toDictionary() -> [String:Any]
        {
            var dictionary = [String:Any]()
            if key != nil{
                dictionary["attribute_code"] = key
            }
            if value != nil{
                dictionary["value"] = value
            }
            return dictionary
        }
    }
}


, но это дает мне ошибку, что "Невозможно преобразовать значение типа 'NSArray' в тип '[String: Any]' в принудительном порядке "

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...