在Swift中搜索对象数组

时间:2018-06-18 06:48:01

标签: ios swift

我有代码:

func getDataToProductView(){
        let documentsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let path = documentsDir.appendingPathComponent((AppGlobalManager.sharedManager.loggedUser?.selectedLanguage)! + "/json/products.json")
        do {
            let data = try Data(contentsOf: path)
            let decoder = JSONDecoder()
            let productsTmpObjectArray = try decoder.decode([Products].self, from: data)

            productCount.text = String(productsObjectArray.count)

        } catch {
            print("ProductViewControler - Error 102: Problem with parse file. \(error)")
        }
    }

struct ProductObject : Codable {
    let palletHeight : Double?
    let layerPallet : Int?
    let prepCombisteamer : String?
    let id : Int?
    let temporaryWorlds : [String]?
    let temporarySegments : [String]?
    let sunFlower : Bool?
    let inPieces : Bool?
    let noBox : Int?
    let prepFryingPan : String?
    let packageContents : Double?
    let carbohydrates : Double?
    let eanBox : String? // Int
    let kcal : Int?
    let markedAsFavourite1 : Bool?
    let temporaryPodSegmentyRynku : [String]?
    let prepPot : String?
    let prepMicrowave : String?
    let boxLayer : Int?
    let code : String? // Int
    let prepDeepFryer : String?
    let name : String?
    let temporaryConcepts : [String]?
    let active : Bool?
    let temporarySegmentyRynku : [String]?
    let shelfLifeTimeFrame : String?
    let changeTime : ChangeTime?
    let palletWeight : Double?
    let markedAsFavourite2 : Bool?
    let kj : Int?
    let langVersions : [LangVersions]?
    let proteins : Double? // Int
    let regions : [Int]?
    let containsGluten : Bool?
    let markedAsFavourite3 : Bool?
    let eanFoil : String? // Int
    let shelfLife : String? // Int
    let contentPerBox : Int?
    let prepOven : String?
}

和示例对象:

{
    "palletHeight": 190,
    "layerPallet": 6,
    "prepCombisteamer": "10-15 min / 220C",
    "id": 152,
    "temporaryWorlds": [
      "ALWAYS_EVERYWHERE"
    ],
    "temporarySegments": [
      "FRIES"
    ],
    "sunFlower": false,
    "inPieces": false,
    "noBox": 54,
    "prepFryingPan": "",
    "packageContents": 2.5,
    "carbohydrates": 24.5,
    "fat": 4,
    "eanBox": "8710449999194",
    "kcal": 150,
    "markedAsFavourite1": false,
    "temporaryPodSegmentyRynku": [
      "DANIE_GLOWNE",
      "BUFET"
    ],
    "prepPot": "",
    "prepMicrowave": "",
    "boxLayer": 9,
    "code": "302503",
    "prepDeepFryer": "3-3,5 min / 175C",
    "name": "CLASSIC Oven Fries 10 mm",
    "temporaryConcepts": [
      "MIX_TO_GO",
      "SHARE_THE_FUN"
    ],
    "active": true,
    "temporarySegmentyRynku": [
      "KANTYNA_CATERING",
      "HOTEL"
    ],
    "shelfLifeTimeFrame": "MONTHS",
    "changeTime": {
      "dayOfMonth": 6,
      "minute": 44,
      "second": 41,
      "year": 2017,
      "month": 3,
      "hourOfDay": 10
    },

我想在我的数组productsTmpObjectArray对象中搜索所有对象:

a) temporaryPodSegmentyRynku == DANIE_GLOWNE
b) temporaryConcepts = SHARE_THE_FUN
c) temporarySegmentyRynku = HOTEL
d) fat = 8

有谁知道怎么做?

我喜欢这样的事情:

func productsFilter1(array: [Products])-> [Products]{
        var tmpProductsArray = [Products]()
        for product in array{
            if product.fat == 8 {
                tmpProductsArray.append(Products(id: product.id, active: product.active, regions:product.regions, code: product.code, eanFoil: product.eanFoil, eanBox: product.eanBox, noBox: product.noBox, layerPallet: product.layerPallet, boxLayer: product.boxLayer, palletWeight: product.palletWeight, palletHeight: product.palletHeight, packageContents: product.packageContents, contentPerBox: product.contentPerBox, name: product.name, inPieces: product.inPieces, prepDeepFryer: product.prepDeepFryer, prepOven: product.prepOven, prepCombisteamer: product.prepCombisteamer, prepFryingPan: product.prepFryingPan, prepPot: product.prepPot, prepMicrowave: product.prepMicrowave, shelfLife: product.shelfLife, shelfLifeTimeFrame: product.shelfLifeTimeFrame, kj: product.kj, kcal: product.kcal, proteins: product.proteins, carbohydrates: product.carbohydrates, fat: product.fat, sunFlower: product.sunFlower, containsGluten: product.containsGluten, pieceWeight: product.pieceWeight, numberOfPiecesInPackage: product.numberOfPiecesInPackage, temporaryWorlds: product.temporaryWorlds, temporaryConcepts: product.temporaryConcepts, temporarySegments: product.temporarySegments, temporaryPodSegmentyRynku: product.temporaryPodSegmentyRynku, temporarySegmentyRynku: product.temporarySegmentyRynku, langVersions: product.langVersions, changeTime: product.changeTime, markedAsFavourite1: product.markedAsFavourite1, markedAsFavourite2: product.markedAsFavourite2, markedAsFavourite3: product.markedAsFavourite3))
            }
        }
        return tmpProductsArray
    }

这个解决方案可以吗? 对于点:a,b,c,最好做单独的搜索功能? 你如何搜索点a,b和c?

2 个答案:

答案 0 :(得分:0)

func productsFilter1(array: [Products])-> [Products]{

    return array.filter({$0.temporaryPodSegmentyRynku.contains("DANIE_GLOWNE") && $0.temporaryConcepts.contains("SHARE_THE_FUN") && $0.temporarySegmentyRynku.contains("HOTEL") && $0.fat == 8})
}

答案 1 :(得分:0)

您可以在数组上使用filter方法来查找匹配项。由于您的数组属性是可选的,因此需要考虑并且我首先在.fat上进行过滤,因为它是最便宜的性能副。

let result = productsTmpObjectArray.filter {($0.fat == 8) &&
   $0.temporaryPodSegmentyRynku?.contains("DANIE_GLOWNE") && 
   $0.temporaryConcepts?.contains("SHARE_THE_FUN") && 
   $0.temporarySegmentyRynku?.contains("HOTEL") }

let result2 = productsTmpObjectArray.filter { $0.prepDeepFryer?.isEmpty }