如何在结构对象子字符串中明智地过滤字符串数组的值

How to filter the values of a string array in a struct object substring wise

我想检查 object.This 部分中是否有一个值,代码工作正常,但如果我只写完整的名称,它将进入已过滤的 object.i 需要获取已过滤的数据当搜索测试与字符串数组中的子字符串匹配时

["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({[=10=].sectionObjects.contains(searchBar.text!)})

请尝试以下操作:

objectArrayFilter = objectArray.filter { [=10=].sectionObjects.contains(where: { [=10=].contains(searchBar.text!) }) }

如果你想要这样的过滤器,如果你在 UITextField 中输入字符串 afg 那么它应该 return 只有两个对象 "Afghan Collie", "Afghan Hound" with section A, 就可以这样.

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

编辑: 您创建的结构不正确您需要做的是创建另一个结构并使用 属性 object、id、ph 和 image all类型的字符串,然后在您的对象结构中创建此结构的数组。

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

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

现在这样过滤。

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

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