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!)})
6
  • 1
    Possible duplicate of Check if array contains part of a string in Swift? Commented Aug 22, 2017 at 12:42
  • Another one: stackoverflow.com/questions/34511918/…. Commented Aug 22, 2017 at 12:43
  • they use arrays' I used structure,so some confusion Commented Aug 22, 2017 at 12:49
  • $0.sectionObjects is the array, and you have to check if some string in that array contains the search text. Commented Aug 22, 2017 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. Commented Aug 23, 2017 at 4:29

2 Answers 2

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
}
Sign up to request clarification or add additional context in comments.

5 Comments

Welcome mate :)
at the time of searching name is coming in the name field of session but all the id 's and images of the section is coming in filteredsection object
From where this Id and images come from there is nothing like that in your question
what if the struct objects contains section_id and sectionoimages
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
1

Please try the following :

objectArrayFilter = objectArray.filter { $0.sectionObjects.contains(where: { $0.contains(searchBar.text!) }) }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.