简体   繁体   中英

Searching Array of dictionary of Array (some model) in iOS

I have a query regarding search in array of dictionary of array.

I am calling a web service & getting response from server, After response I created a model name 'Merchant Model' & place all the data in order.

Here is MerchantModel

struct MerchantModel {
    let idString : UInt
    var squareImageUrlString : String
    let rectangleImageUrlString : String
    let nameString : String
    let stateDescriptionString : String

}

Below is the data that I have created from service using MerchantModel

    [
        ["D": [MerchantModel(idString: 70000032, squareImageUrlString: "http:wigroup/wiplatform/uploads/2016-09-26_153901_1474904341_68899.png?uid=1474904346379", rectangleImageUrlString: "http://latform/uploads/2016-09-26_153556_1474904156_79221.jpg?uid=1474904164349", nameString: "ABCD ", stateDescriptionString: "Active")]],
        ["K": [MerchantModel(idString: 22, squareImageUrlString: "http://wigroup/wigroup/wiplatform/uploads/2017-01-12_083720_1484210240_77692.jpg?uid=1484210329594", rectangleImageUrlString: "http://r/uploads/2017-01-12_084109_1484210469_83189.jpg?uid=1484210491620", nameString: "ABCD Online", stateDescriptionString: "Active")]],
        ["P": [MerchantModel(idString: 70000013, squareImageUrlString: "http://form/uploads/2016-11-02_093929_1478079569_70996.jpg?uid=1478079577342", rectangleImageUrlString: "http://rad2rm/uploads/2016-11-02_093948_1478079588_71976.jpg?uid=1478079594170", nameString: "XYZ \'n Pay", stateDescriptionString: "Active"), MerchantModel(idString: 70000068, squareImageUrlString: "http://igroup/wiplatform/uploads/2016-08-11_102601_1470911161_64234.png", rectangleImageUrlString: "http://roup/wiplatform/uploads/2016-08-11_102632_1470911192_47769.png", nameString: "XABCD Pay ", stateDescriptionString: "Active")]],
        ["R": [MerchantModel(idString: 19, squareImageUrlString: "http://group/wiplatform/uploads/2015-06-09_133237_1433856757_50883.jpg", rectangleImageUrlString: "http://raads/2015-06-09_133217_1433856737_47358.jpg", nameString: "Test", stateDescriptionString: "Active")]]
]

Now I have to perform search over this array. I wrote below code as

 func filter(array : [[String : [MerchantModel]]], byString filterString : String) -> [[String : [MerchantModel]]]? {
  return array.filter({ (dictData : [String : [MerchantModel]]) -> Bool in
                let values = dictData.values
                values.filter({ ([MerchantModel]) -> Bool in
                    $0.nameString == filterString
                })
            })
    }

But getting error as

"Value of type '[MerchantModel]' has no member 'nameString'"

Please suggest me how to perform search on this array. I am trying to avoid loops (for, while etc) in Swift.

Replace:

values.filter({ ([MerchantModel]) -> Bool in
    $0.nameString == filterString
})

With:

values.filter({ ([MerchantModel]) -> Bool in
    $0.filter({ (MerchantModel) -> Bool in
        $0.nameString == filterString
    )}
})

The problem is you are initializing a single object array

["D": [MerchantModel(idString: 70000032, squareImageUrlString: "http:wigroup/wiplatform/uploads/2016-09-26_153901_1474904341_68899.png?uid=1474904346379", rectangleImageUrlString: "http://latform/uploads/2016-09-26_153556_1474904156_79221.jpg?uid=1474904164349", nameString: "ABCD ", stateDescriptionString: "Active")]

and than trying to get nameString from it.

To loop through array use smth like

func filter(array : [[String : [NSDictionary]]], byString filterString : String) -> [[String : [NSDictionary]]]? {
    return array.filter({ (dictData : [String : [NSDictionary]]) -> Bool in
        let values = dictData.values
        $0.filter({ (MerchantModel) -> Bool in
            $0.nameString == filterString
        )}
    })
}

Inner filters must return boolean. Try this.

func filter(array : [[String : [MerchantModel]]], byString filterString : String) -> [[String : [MerchantModel]]]? {
    return array.filter { (dictData) -> Bool in
        dictData.values.filter({ (subarray) -> Bool in
            subarray.filter { $0.nameString == filterString }.count > 0
        }).count > 0
    }
}

And one liner

return array.filter { $0.values.filter { $0.filter { $0.nameString == filterString }.count > 0 }.count > 0 }

Playground test results.

let array = [["D": [MerchantModel(idString: 70000032, squareImageUrlString: "http:wigroup/wiplatform/uploads/2016-09-26_153901_1474904341_68899.png?uid=1474904346379", rectangleImageUrlString: "http://latform/uploads/2016-09-26_153556_1474904156_79221.jpg?uid=1474904164349", nameString: "ABCD ", stateDescriptionString: "Active")]],
             ["K": [MerchantModel(idString: 22, squareImageUrlString: "http://wigroup/wigroup/wiplatform/uploads/2017-01-12_083720_1484210240_77692.jpg?uid=1484210329594", rectangleImageUrlString: "http://r/uploads/2017-01-12_084109_1484210469_83189.jpg?uid=1484210491620", nameString: "ABCD Online", stateDescriptionString: "Active")]],
             ["P": [MerchantModel(idString: 70000013, squareImageUrlString: "http://form/uploads/2016-11-02_093929_1478079569_70996.jpg?uid=1478079577342", rectangleImageUrlString: "http://rad2rm/uploads/2016-11-02_093948_1478079588_71976.jpg?uid=1478079594170", nameString: "XYZ \'n Pay", stateDescriptionString: "Active"), MerchantModel(idString: 70000068, squareImageUrlString: "http://igroup/wiplatform/uploads/2016-08-11_102601_1470911161_64234.png", rectangleImageUrlString: "http://roup/wiplatform/uploads/2016-08-11_102632_1470911192_47769.png", nameString: "XABCD Pay ", stateDescriptionString: "Active")]],
             ["R": [MerchantModel(idString: 19, squareImageUrlString: "http://group/wiplatform/uploads/2015-06-09_133237_1433856757_50883.jpg", rectangleImageUrlString: "http://raads/2015-06-09_133217_1433856737_47358.jpg", nameString: "Test", stateDescriptionString: "Active")]]]

let filterString = "ABCD "
if let results = filter(array: array, byString: filterString) {
    print(results)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM