0

I want to check a value is there in section object.This code is working fine but if I write the complete name only it will get in to the filtered object.i need to get the filtered data when the search test matches with a substring in the string array

["A": ["Affenpoo", "Affenpug", "Affenshire", "Affenwich", "Afghan Collie", "Afghan Hound"], "B": ["Bagle Hound", "Boxer"]]

        struct Objects {
             var sectionName : String!
             var sectionObjects : [String] 
             var sectionid:[String]!
             var sectionph:[String]!
             var sectionImage:[String]!
                }

        var objectArray = [Objects]()
        var objectArrayFilter = [Objects]()

    objectArrayFilter = objectArray.filter({$0.sectionObjects.contains(searchBar.text!)})
SHINTO JOSEPH
  • 377
  • 4
  • 17
  • 1
    Possible duplicate of [Check if array contains part of a string in Swift?](https://stackoverflow.com/questions/30828074/check-if-array-contains-part-of-a-string-in-swift) – Martin R Aug 22 '17 at 12:42
  • Another one: https://stackoverflow.com/questions/34511918/swift-2-filter-array-of-strings-including-like-condition. – Martin R Aug 22 '17 at 12:43
  • they use arrays' I used structure,so some confusion – SHINTO JOSEPH Aug 22 '17 at 12:49
  • `$0.sectionObjects` is the array, and you have to check if some string in that array contains the search text. – Martin R Aug 22 '17 at 14:21
  • @SHINTOJOSEPH Accepted answer will return all object in inner array sectionObjects if you search for specific item, Or you want something like that if you enter in textfiled **afg** then it should return only two object **"Afghan Collie", "Afghan Hound"** with section A, I'm asking this because accepted answer will return all objects of in sectionObjects array. – Nirav D Aug 23 '17 at 04:29
  • @NiravD yea accepted answer is returning all the objects in section objects array.How to fix that – SHINTO JOSEPH Aug 23 '17 at 05:06

2 Answers2

1

Please try the following :

objectArrayFilter = objectArray.filter { $0.sectionObjects.contains(where: { $0.contains(searchBar.text!) }) }
Vini App
  • 7,339
  • 2
  • 26
  • 43
1

If you want filter like that if you enter string afg in UITextField then it should return only two object "Afghan Collie", "Afghan Hound" with section A, then you can make it like this.

objectArrayFilter = objectArray.flatMap {
    var filterObjects = $0
    filterObjects.sectionObjects = $0.sectionObjects.filter { 
        $0.range(of : searchBar.text!, options: .caseInsensitive) != nil
    }
    return filterObjects.sectionObjects.isEmpty ? nil : filterObjects
} 

Edit: Struct that you have make is not proper what you need to do is make another struct and make with property object,id,ph and image all type of string and then make array of this struct inside your Object struct.

struct SubObjects {
    var sectionObject: String!
    var sectionid: String!
    var sectionph: String!
    var sectionImage: String!
}

struct Objects {
    var sectionName : String!
    var sectionObjects : [SubObjects]! 
}

Now filter this way.

var objectArray = [Objects]()
var objectArrayFilter = [Objects]()

objectArrayFilter = objectArray.flatMap {
    var filterObjects = $0
    filterObjects.sectionObjects = $0.sectionObjects.filter { 
        $0.sectionObject.range(of : searchBar.text!, options: .caseInsensitive) != nil
    }
    return filterObjects.sectionObjects.isEmpty ? nil : filterObjects
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183