使用Swift将具有相同类型的字典分组为具有完整键和值的数组

Bad_Developer

我已经很久没有使用Swift了。

我有一个这样的响应数据,并将其保存到一个名为的数组中responseData

[
{
"type": "Switch",
"name": "Switch1",
"search_key": "is_promotion",
"key": "is_promotion=1",
"icon": ""
},
{
"type": "Switch",
"name": "Switch2",
"search_key": "shop_type",
"key": "shop_type=2",
"icon": ""
},
{
"type": "Switch",
"name": "Switch3",
"search_key": "is_certified",
"key": "is_certified=1",
"icon": ""
},
{
"type": "Switch",
"name": "Switch4",
"search_key": "shop_free_shipping",
"key": "shop_free_shipping=1",
"icon": ""
},
{
"type": "Switch",
"name": "Switch5",
"search_key": "is_loyalty",
"key": "is_loyalty=1",
"icon": ""
},
{
"type": "Switch",
"name": "Switch6",
"search_key": "is_using_instant",
"key": "is_using_instant=1",
"icon": ""
},
{
"type": "Switch",
"name": "Switch7",
"search_key": "is_installment",
"key": "is_installment=1",
"icon": ""
},
{
"type": "Range",
"name": "Price Range",
"search_key": "level_Price_Max_Min",
"value": [
{
"option_id": 0,
"option_name": 0
},
{
"option_id": 0,
"option_name": 10000000
}
]
},
{
"type": "ColorTerm",
"name": "Color",
"search_key": "color_key",
"value": [
{
"option_id": 605,
"value": "Black",
"color_id": 13,
"image": "",
"option_name": "Black",
"background": "#000000",
"option_active": "",
"facet_count": 52655
},

现在,我想将所有具有类型的字典分组Switch到一个数组中,然后可以访问其中的键,然后SwitchUITableView具有2个部分的a上显示两个类型的数组和其他类型的数据Switch键入0部分)。我该怎么做?我必须搜索其他解决方案,但我不知道如何将其应用于我的代码上班。

这是我的FilterModel类:

class FilterModel: NSObject, NSCoding, NSCopying {

    override func copy(with zone: NSZone? = nil) -> Any {
        // This is the reason why `init(_ model: GameModel)`
        // must be required, because `GameModel` is not `final`.
        let copy = FilterModel(dict: self.dictionary)

        if let arrAttribute = NSArray(array: self.value , copyItems: true) as? [AttributeValueModel] {
            copy.value = arrAttribute
        }

        return copy
    }

    override init(dict: Dictionary<String, Any>) {
        super.init(dict: dict);

        value = self.valueParse()
    }


    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    var name: String? {
        return self.dictionary.getString(forKey: "name")
    }

    var icon: String? {
        return self.dictionary.getString(forKey: "icon")
    }

    var search_key: String? {
        return self.dictionary.getString(forKey: "search_key")
    }

    var key: String? {
        return self.dictionary.getString(forKey: "key")
    }

    var type: FilterDisplayType {
        let type = self.dictionary.getString(forKey: "type")
        return self.getType(string: type)
    }

    var value: [AttributeValueModel] = [];


    func valueParse()-> [AttributeValueModel] {
        // with switch type, Just set true or false
        // Change ParentValue to Child
        if type == .Switch {
            let dict:Dictionary<String, AnyObject> = [
                "option_id": "false" as AnyObject,
                "option_name": self.name! as AnyObject,
                "name": self.name! as AnyObject,
                "icon": self.icon! as AnyObject
            ]

            let item = AttributeValueModel(dict:dict);
            return [item]
        }

        guard let childs  = (self.dictionary["value"]) as? [Dictionary<String, AnyObject>]
            else { return [] }

        var output: [AttributeValueModel] = [];

        for  aDict in childs {
            let item = AttributeValueModel(dict:aDict);

            if type == .Range  && item.option_id == "0" {
                item.setRangeOptionID(aValue: item.option_name!)
            }

            output.append(item);
        }

        return output;
    }

    ///get list AttributeValueModel Selected
    func selectedValues() -> [AttributeValueModel] {

        var output: [AttributeValueModel] = [];

        for itemTemp in self.value {
            if(itemTemp.selected){
                if type == .Switch {
                    itemTemp.setSelectedOptionID()
                }

                output.append(itemTemp);
            }
        }
        return output;
    }

    /// make a Filter Item from attributeValues Seleted
    func getFilterItem() -> FilterItem? {

        var itemFilter: FilterItem = FilterItem(key: self.search_key!, value: "")
        itemFilter.key = self.search_key!

        let output: NSMutableArray = [];

        for attItem in self.selectedValues() {
            if attItem.option_id != "" {
                output.add(attItem.option_id!);
            }
        }

        if(output.count == 0) {
            return nil;
        }

        let valueString = output.componentsJoined(by: ",");

        itemFilter.value = valueString;

        return itemFilter
    }

    ///get list AttributeValueModel Selected
    func resetToDefault() -> [AttributeValueModel] {

        var output: [AttributeValueModel] = [];

        for itemTemp in self.value {
            if(itemTemp.selected){
                itemTemp.selected = false

                if type == .Switch {
                    itemTemp.setSelectedOptionID()
                }

                if type == .Range {
                    itemTemp.setRangeOptionID(aValue: itemTemp.option_name!)
                }

                output.append(itemTemp);
            }
        }
        return output;
    }

    //for UI
    var wasExpanding = false
    var numberOfRow:Int = 0
    /************/

    var attributeNameLength: Int {
        var string = ""
        for item in valueParse() {
            string += item.option_name!
        }

        return string.count
    }

    var lenghtSizeName:Int {

        var row:Int = 1
        var width:CGFloat = 0
        let padding:CGFloat = 8

        let screen = screenWidth - 50 - 16
        for item in valueParse() {
            let size = ((item.option_name ?? "") as NSString).size(withAttributes: [
                NSAttributedStringKey.font : UIFont.fontRegular_Big()
                ])

            let totalWidth = size.width + padding + 16

            if totalWidth <= CGFloat(32) {
                width += 32
                if width >= screen {
                    row += 1
                    width = 32
                }
            } else {
                width += totalWidth
                if width >= screen {
                    row += 1
                    width = totalWidth
                }
            }
        }
        return row
    }

}

您可以过滤响应数据以获取阵列中的开关。

responseData.filter {($0.type ?? "") == "Switch"}

当然,!=会给您带来非切换。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TypeScript:如何为具有许多相同类型的键和相同类型的值的对象创建接口?

将具有相同值的对象键分组

与数据框具有相同键和值的字典列表

添加具有相同键和int类型值的字典列表

具有不同值字典的相同键

将数组值与特定键组合具有相同的值

将字典转换为具有相同键、值和布局的列表

为所有值都具有相同类型的对象定义类型

将字典列扩展为具有字典键和字典值的行作为熊猫中的新列值

如何在Python中为具有不同类型的字典添加类型声明作为值

使用正则表达式将字符串分解为具有键和值的字典

聚合具有相同值的相同类型节点的属性

将具有值的对象分组为索引数组

将具有相同值的行分组

将具有多个键的字典数组减少到 Swift 中具有单个键的字典数组

如何将 React 数组转换为具有相同键和值的 JSON 对象?

使用LINQ将具有相同值的行分组为列

如何在python中合并两个具有相同键和数组类型值的嵌套字典?

如何键入提示具有不同类型值的字典

如何将具有多个文本列表值的python字典拆分为具有相同值的键的单独字典?

Python:具有一个键和多个值的字典:如何获取具有相同SET值的键列表?

选择查询具有相同类型值的多列

使用jQuery具有相同键和不同值的对象数组

如何仅使用注释将具有相同类型的bean组合到一个数组中?

熊猫-将具有字典的列分为具有键和值的两列

如何将具有相同键:值的 mongo 文档分组并以数组的形式返回响应?

将值字典附加到具有相同键的列表字典中

如何将任何对象的列表转换为具有相同类型的数组?

将键和值对添加到具有重复键的字典中的字典中