Swift Mullti Уровень фильтрации - PullRequest
0 голосов
/ 18 января 2020
    {
    "productId" : "prod830028",
    "deviceFeatures" : [
        "SIM Type:Nano",
        "Battery:3,174mAh",
        "Front Camera:7MP",
        "Rear Camera:12MP + 12MP",
        "OS:iOS 12",
        "Processor:A12 Bionic chip with next-generation Neural Engine",
        "Display:6.5 inches, OLED"
    ],

    "productOverView": "Meet the iPhone X - the device that’s so smart that it responds to a tap, your voice, and even a glance. Elegantly designed with a large 14.73 cm (5.8) Super Retina screen and a durable front-and-back glass.",
    "specifications":[
        {
            "key": "Color",
            "value": "Space Grey"
        },
        {
            "key": "Internal Memory",
            "value": "64 GB"
        },
        {
            "key": "Operating System",
            "value": "iOS 11.1.1"
        },
        {
            "key": "Primary Camera",
            "value": "12 MP"
        },
        {
            "key": "RAM",
            "value": "5.8 inch"
        },
        {
            "key": "Screen Size",
            "value": "15"
        },
        {
            "key": "Expandable Memory",
            "value": "No"
        },
        {
            "key": "Camera Feature",
            "value": "Geo-tagging, simultaneous 4K video and 8MP image recording, touch focus, face/smile detection, HDR (photo/panorama)"
        }
    ],
    "preorderable" : false,
    "responseMsg" : "SUCCESS",
    "skuDetails" : [
        {
            "oldPriceWithVat" : "4461.45",
            "pointsWithVat" : "393900",
            "pointsWithoutVat" : "375143",
            "ratePlanCode" : "RPDLAPAPPEQ000000G9F",
            "priceWithouthVat" : "3751.43",
            "priceWithVat" : "3939.0015",
            "devicePaymentOptions" : [
                {
                    "price" : "362.0",
                    "duration" : "12",
                    "demandId" : "3052451"
                },
                {
                    "price" : "185.0",
                    "duration" : "24",
                    "demandId" : "3052468"
                },
                {
                    "price" : "244.0",
                    "duration" : "18",
                    "demandId" : "3052593"
                }
            ],
            "payWithPointAvailable" : false,
            "skuId" : "bgSku3820202391",
            "vatPercentage" : "5",
            "sicCode" : "3820202391",
            "oldPriceWithoutVat" : "4249.0",
            "oldPricePointsWithoutVat" : "424900",
            "cashOnDeliveryAvailable" : true,
            "images" : [
                "https:\/\/www.etisalat.ae\/en\/system\/wst\/assets\/img\/devices\/AP-iPhoneXSMax\/AP-iPhoneXsMax-SpaceGray-PureAngles-500.jpg"
            ],
            "skuCoreProperties" : [
                {
                    "colorCode" : "#8C8C8C",
                    "value" : "Space Gray",
                    "key" : "Colour",
                    "displayText" : "Colour"
                },
                {
                    "key" : "Storage",
                    "value" : "64GB",
                    "displayText" : "Storage"
                },
                {
                    "key" : "Memory",
                    "value" : "4GB RAM",
                    "displayText" : "Memory"
                }
            ],
            "oldPricePointsWithVat" : "446145",
            "demandId" : "3060416"
        },
        {
            "oldPriceWithVat" : "4461.45",
            "pointsWithVat" : "316575",
            "pointsWithoutVat" : "301500",
            "ratePlanCode" : "RPDLAPAPPEQ000000G9I",
            "priceWithouthVat" : "3015.0",
            "priceWithVat" : "3165.75",
            "devicePaymentOptions" : [
                {
                    "price" : "362.0",
                    "duration" : "12",
                    "demandId" : "3052402"
                },
                {
                    "price" : "185.0",
                    "duration" : "24",
                    "demandId" : "3052634"
                },
                {
                    "price" : "244.0",
                    "duration" : "18",
                    "demandId" : "3052559"
                }
            ],
            "payWithPointAvailable" : false,
            "skuId" : "bgSku3820202404",
            "vatPercentage" : "5",
            "sicCode" : "3820202404",
            "oldPriceWithoutVat" : "4249.0",
            "oldPricePointsWithoutVat" : "424900",
            "cashOnDeliveryAvailable" : true,
            "images" : [
                "https:\/\/www.etisalat.ae\/en\/system\/wst\/assets\/img\/devices\/AP-iPhoneXSMax\/AP-iPhoneXsMax-Silver-PureAngles-500.jpg"
            ],
            "skuCoreProperties" : [
                {
                    "colorCode" : "#C0C0C0",
                    "value" : "Silver",
                    "key" : "Colour",
                    "displayText" : "Colour"
                },
                {
                    "key" : "Storage",
                    "value" : "64GB",
                    "displayText" : "Storage"
                },
                {
                    "key" : "Memory",
                    "value" : "4GB RAM",
                    "displayText" : "Memory"
                }
            ],
            "oldPricePointsWithVat" : "446145",
            "demandId" : "3060419"
        },
        {
            "oldPriceWithVat" : "4461.45",
            "pointsWithVat" : "393900",
            "pointsWithoutVat" : "375143",
            "ratePlanCode" : "RPDLAPAPPEQ000000GAL",
            "priceWithouthVat" : "3751.43",
            "priceWithVat" : "3939.0015",
            "devicePaymentOptions" : [
                {
                    "price" : "185.0",
                    "duration" : "24",
                    "demandId" : "3052605"
                },
                {
                    "price" : "362.0",
                    "duration" : "12",
                    "demandId" : "3052640"
                },
                {
                    "price" : "244.0",
                    "duration" : "18",
                    "demandId" : "3052646"
                }
            ],
            "payWithPointAvailable" : false,
            "skuId" : "bgSku3820202419",
            "vatPercentage" : "5",
            "sicCode" : "3820202419",
            "oldPriceWithoutVat" : "4249.0",
            "oldPricePointsWithoutVat" : "424900",
            "cashOnDeliveryAvailable" : true,
            "images" : [
                "https:\/\/www.etisalat.ae\/en\/system\/wst\/assets\/img\/devices\/AP-iPhoneXSMax\/AP-iPhoneXsMax-Gold-PureAngles-500.jpg"
            ],
            "skuCoreProperties" : [
                {
                    "colorCode" : "#D4AF37",
                    "value" : "Gold",
                    "key" : "Colour",
                    "displayText" : "Colour"
                },
                {
                    "key" : "Storage",
                    "value" : "64GB",
                    "displayText" : "Storage"
                },
                {
                    "key" : "Memory",
                    "value" : "4GB RAM",
                    "displayText" : "Memory"
                }
            ],
            "oldPricePointsWithVat" : "446145",
            "demandId" : "3060494"
        },
        {
            "oldPriceWithVat" : "5065.2",
            "pointsWithVat" : "445500",
            "pointsWithoutVat" : "424286",
            "ratePlanCode" : "RPDLAPAPPEQ000000G9E",
            "priceWithouthVat" : "4242.86",
            "priceWithVat" : "4455.003",
            "devicePaymentOptions" : [
                {
                    "price" : "210.0",
                    "duration" : "24",
                    "demandId" : "3052406"
                },
                {
                    "price" : "277.0",
                    "duration" : "18",
                    "demandId" : "3052372"
                },
                {
                    "price" : "411.0",
                    "duration" : "12",
                    "demandId" : "3052571"
                }
            ],
            "payWithPointAvailable" : false,
            "skuId" : "bgSku3820202438",
            "vatPercentage" : "5",
            "sicCode" : "3820202438",
            "oldPriceWithoutVat" : "4824.0",
            "oldPricePointsWithoutVat" : "482400",
            "cashOnDeliveryAvailable" : true,
            "images" : [
                "https:\/\/www.etisalat.ae\/en\/system\/wst\/assets\/img\/devices\/AP-iPhoneXSMax\/AP-iPhoneXsMax-SpaceGray-PureAngles-500.jpg"
            ],
            "skuCoreProperties" : [
                {
                    "colorCode" : "#8C8C8C",
                    "value" : "Space Gray",
                    "key" : "Colour",
                    "displayText" : "Colour"
                },
                {
                    "key" : "Storage",
                    "value" : "256GB",
                    "displayText" : "Storage"
                },
                {
                    "key" : "Memory",
                    "value" : "4GB RAM",
                    "displayText" : "Memory"
                }
            ],
            "oldPricePointsWithVat" : "506520",
            "demandId" : "3060415"
        },
        {
            "oldPriceWithVat" : "5065.2",
            "pointsWithVat" : "445500",
            "pointsWithoutVat" : "424286",
            "ratePlanCode" : "RPDLAPAPPEQ000000G9Y",
            "priceWithouthVat" : "4242.86",
            "priceWithVat" : "4455.003",
            "devicePaymentOptions" : [
                {
                    "price" : "210.0",
                    "duration" : "24",
                    "demandId" : "3052650"
                },
                {
                    "price" : "411.0",
                    "duration" : "12",
                    "demandId" : "3052353"
                },
                {
                    "price" : "277.0",
                    "duration" : "18",
                    "demandId" : "3052520"
                }
            ],
            "payWithPointAvailable" : false,
            "skuId" : "bgSku3820202453",
            "vatPercentage" : "5",
            "sicCode" : "3820202453",
            "oldPriceWithoutVat" : "4824.0",
            "oldPricePointsWithoutVat" : "482400",
            "cashOnDeliveryAvailable" : true,
            "images" : [
                "https:\/\/www.etisalat.ae\/en\/system\/wst\/assets\/img\/devices\/AP-iPhoneXSMax\/AP-iPhoneXsMax-Silver-PureAngles-500.jpg"
            ],
            "skuCoreProperties" : [
                {
                    "colorCode" : "#C0C0C0",
                    "value" : "Silver",
                    "key" : "Colour",
                    "displayText" : "Colour"
                },
                {
                    "key" : "Storage",
                    "value" : "256GB",
                    "displayText" : "Storage"
                },
                {
                    "key" : "Memory",
                    "value" : "4GB RAM",
                    "displayText" : "Memory"
                }
            ],
            "oldPricePointsWithVat" : "506520",
            "demandId" : "3060445"
        },
        {
            "oldPriceWithVat" : "5065.2",
            "pointsWithVat" : "445500",
            "pointsWithoutVat" : "424286",
            "ratePlanCode" : "RPDLAPAPPEQ000000G9B",
            "priceWithouthVat" : "4242.86",
            "priceWithVat" : "4455.003",
            "devicePaymentOptions" : [
                {
                    "price" : "411.0",
                    "duration" : "12",
                    "demandId" : "3052561"
                },
                {
                    "price" : "277.0",
                    "duration" : "18",
                    "demandId" : "3052510"
                },
                {
                    "price" : "210.0",
                    "duration" : "24",
                    "demandId" : "3052448"
                }
            ],
            "payWithPointAvailable" : false,
            "skuId" : "bgSku3820202469",
            "vatPercentage" : "5",
            "sicCode" : "3820202469",
            "oldPriceWithoutVat" : "4824.0",
            "oldPricePointsWithoutVat" : "482400",
            "cashOnDeliveryAvailable" : true,
            "images" : [
                "https:\/\/www.etisalat.ae\/en\/system\/wst\/assets\/img\/devices\/AP-iPhoneXSMax\/AP-iPhoneXsMax-Gold-PureAngles-500.jpg"
            ],
            "skuCoreProperties" : [
                {
                    "colorCode" : "#D4AF37",
                    "value" : "Gold",
                    "key" : "Colour",
                    "displayText" : "Colour"
                }
            ],
            "oldPricePointsWithVat" : "506520",
            "demandId" : "3060412"
        },
        {
            "oldPriceWithVat" : "5884.2",
            "pointsWithVat" : "525500",
            "pointsWithoutVat" : "500476",
            "ratePlanCode" : "RPDLAPAPPEQ000000GA0",
            "priceWithouthVat" : "5004.76",
            "priceWithVat" : "5254.9980000000005",
            "devicePaymentOptions" : [
                {
                    "price" : "336.0",
                    "duration" : "18",
                    "demandId" : "3052601"
                },
                {
                    "price" : "499.0",
                    "duration" : "12",
                    "demandId" : "3052569"
                },
                {
                    "price" : "255.0",
                    "duration" : "24",
                    "demandId" : "3052361"
                }
            ],
            "payWithPointAvailable" : false,
            "skuId" : "bgSku3820202486",
            "vatPercentage" : "5",
            "sicCode" : "3820202486",
            "oldPriceWithoutVat" : "5604.0",
            "oldPricePointsWithoutVat" : "560400",
            "cashOnDeliveryAvailable" : true,
            "images" : [
                "https:\/\/www.etisalat.ae\/en\/system\/wst\/assets\/img\/devices\/AP-iPhoneXSMax\/AP-iPhoneXsMax-SpaceGray-PureAngles-500.jpg"
            ],
            "skuCoreProperties" : [
                {
                    "colorCode" : "#8C8C8C",
                    "value" : "Space Gray",
                    "key" : "Colour",
                    "displayText" : "Colour"
                },
                {
                    "key" : "Storage",
                    "value" : "512GB",
                    "displayText" : "Storage"
                },
                {
                    "key" : "Memory",
                    "value" : "4GB RAM",
                    "displayText" : "Memory"
                }
            ],
            "oldPricePointsWithVat" : "588420",
            "demandId" : "3060451"
        },
        {
            "oldPriceWithVat" : "5884.2",
            "pointsWithVat" : "525500",
            "pointsWithoutVat" : "500476",
            "ratePlanCode" : "RPDLAPAPPEQ000000G9O",
            "priceWithouthVat" : "5004.76",
            "priceWithVat" : "5254.9980000000005",
            "devicePaymentOptions" : [
                {
                    "price" : "255.0",
                    "duration" : "24",
                    "demandId" : "3052466"
                },
                {
                    "price" : "499.0",
                    "duration" : "12",
                    "demandId" : "3052319"
                },
                {
                    "price" : "336.0",
                    "duration" : "18",
                    "demandId" : "3052334"
                }
            ],
            "payWithPointAvailable" : false,
            "skuId" : "bgSku3820202505",
            "vatPercentage" : "5",
            "sicCode" : "3820202505",
            "oldPriceWithoutVat" : "5604.0",
            "oldPricePointsWithoutVat" : "560400",
            "cashOnDeliveryAvailable" : true,
            "images" : [
                "https:\/\/www.etisalat.ae\/en\/system\/wst\/assets\/img\/devices\/AP-iPhoneXSMax\/AP-iPhoneXsMax-Silver-PureAngles-500.jpg"
            ],
            "skuCoreProperties" : [
                {
                    "colorCode" : "#C0C0C0",
                    "value" : "Silver",
                    "key" : "Colour",
                    "displayText" : "Colour"
                },
                {
                    "key" : "Storage",
                    "value" : "512GB",
                    "displayText" : "Storage"
                },
                {
                    "key" : "Memory",
                    "value" : "4GB RAM",
                    "displayText" : "Memory"
                }
            ],
            "oldPricePointsWithVat" : "588420",
            "demandId" : "3060427"
        },
        {
            "oldPriceWithVat" : "5884.2",
            "pointsWithVat" : "525500",
            "pointsWithoutVat" : "500476",
            "ratePlanCode" : "RPDLAPAPPEQ000000G9S",
            "priceWithouthVat" : "5004.76",
            "priceWithVat" : "5254.9980000000005",
            "devicePaymentOptions" : [
                {
                    "price" : "499.0",
                    "duration" : "12",
                    "demandId" : "3052340"
                },
                {
                    "price" : "336.0",
                    "duration" : "18",
                    "demandId" : "3052613"
                },
                {
                    "price" : "255.0",
                    "duration" : "24",
                    "demandId" : "3052324"
                }
            ],
            "payWithPointAvailable" : false,
            "skuId" : "bgSku3820202524",
            "vatPercentage" : "5",
            "sicCode" : "3820202524",
            "oldPriceWithoutVat" : "5604.0",
            "oldPricePointsWithoutVat" : "560400",
            "cashOnDeliveryAvailable" : true,
            "images" : [
                "https:\/\/www.etisalat.ae\/en\/system\/wst\/assets\/img\/devices\/AP-iPhoneXSMax\/AP-iPhoneXsMax-Gold-PureAngles-500.jpg"
            ],
            "skuCoreProperties" : [
                {
                    "colorCode" : "#D4AF37",
                    "value" : "Gold",
                    "key" : "Colour",
                    "displayText" : "Colour"
                }
            ],
            "oldPricePointsWithVat" : "588420",
            "demandId" : "3060430"
        }
    ],
    "brand" : "Apple",
    "responseCode" : "0000000",
    "name" : "iPhone XS Max"
}

у меня выше json. то, что я хочу, это использовать фильтр, чтобы получить skudetails объекта только для тех, чей цвет золота и объем хранилища составляет 64 ГБ. это может быть достигнуто через фильтр? Что касается фильтрации первого уровня, она работает нормально для меня, как это:

 if let skuDetailArray = self.deviceSKUsAndDetailsResponse?.skuDetails
 let demandId =  skuDetailArray.first(where: { $0.demandId == "3060416" })

, но я не смог разобрать данные для указанных выше условий, и если это невозможно, через это можно сделать с помощью предиката или любого другого другой метод, чем, пожалуйста, дайте мне знать.

**

  • ОБНОВЛЕНИЕ: показывает класс для этих моделей

**

class SWGetDeviceSKUsAndDetailsResponseModel : SWBaseResponseModel {

    let brand : String?
    let deviceFeatures : [String]?
    let name : String?
    let preorderable : Bool?
    let productId : String?
    let productOverView : String?
    let skuDetails : [SWSkuDetail]?
    let specifications : [SWSpecification]?

}

класс SWSkuDetail: Codable {

let cashOnDeliveryAvailable : Bool?
let demandId : String?
let devicePaymentOptions : [SWDevicePaymentOption]?
let images : [String]?
let oldPricePointsWithVat : String?
let oldPricePointsWithoutVat : String?
let oldPriceWithVat : String?
let oldPriceWithoutVat : String?
let payWithPointAvailable : Bool?
let pointsWithVat : String?
let pointsWithoutVat : String?
let priceWithVat : String?
let priceWithouthVat : String?
let ratePlanCode : String?
let sicCode : String?
let skuCoreProperties : [SWSkuCoreProperty]?
let skuId : String?
let vatPercentage : String?

}

класс SWSkuCoreProperty: кодируемый {

let colorCode : String?
let displayText : String?
let key : String?
let value : String?

}

1 Ответ

1 голос
/ 18 января 2020

Следующий код вернет любое значение SWSkuDetail, которое имеет Color = Gold и Storage = 64 ГБ.

Функция фильтрации сначала отфильтровывает элементы с заданными ключами, а затем проверяет, что она получила 2 хиты и что оба значения существуют в этих двух.

let keyColor = "Colour"
let keyStorage = "Storage"

let filtered = skuDetailArray.filter { detail in
    guard let props = detail.skuCoreProperties else { return false }
    let array = props.filter { $0.key == keyColor || $0.key == keyStorage }.map {$0.value}
    return array.count == 2 && array.contains("Gold") && array.contains("64GB")
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...