Struct 如何在结构对象子字符串中筛选字符串数组的值

Struct 如何在结构对象子字符串中筛选字符串数组的值,struct,swift3,uisearchbar,uisearchcontroller,Struct,Swift3,Uisearchbar,Uisearchcontroller,我想检查节对象中是否有值。此代码运行正常,但如果我只写入完整名称,它将进入筛选对象。我需要在搜索测试与字符串数组中的子字符串匹配时获取筛选数据 ["A": ["Affenpoo", "Affenpug", "Affenshire", "Affenwich", "Afghan Collie", "Afghan Hound"], "B": ["Bagle Hound", "Boxer"]] struct Objects { var sectionName

我想检查节对象中是否有值。此代码运行正常,但如果我只写入完整名称,它将进入筛选对象。我需要在搜索测试与字符串数组中的子字符串匹配时获取筛选数据

["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!)})

请尝试以下操作:

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

请尝试以下操作:

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

如果您想要这样的过滤器,如果您在
UITextField
中输入字符串
afg
,那么它应该只返回两个对象“阿富汗牧羊犬”、“阿富汗猎犬”,并带有
A
,然后您可以这样做

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
} 

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

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 = $0
    filterObjects.sectionObjects = $0.sectionObjects.filter { 
        $0.sectionObject.range(of : searchBar.text!, options: .caseInsensitive) != nil
    }
    return filterObjects.sectionObjects.isEmpty ? nil : filterObjects
}

如果您想要这样的过滤器,如果您在
UITextField
中输入字符串
afg
,那么它应该只返回两个对象“阿富汗牧羊犬”、“阿富汗猎犬”,并带有
A
,然后您可以这样做

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
} 

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

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 = $0
    filterObjects.sectionObjects = $0.sectionObjects.filter { 
        $0.sectionObject.range(of : searchBar.text!, options: .caseInsensitive) != nil
    }
    return filterObjects.sectionObjects.isEmpty ? nil : filterObjects
}

可能与另一个重复:。它们使用数组的“我使用的结构”,因此有些混淆
$0。sectionObjects
是数组,您必须检查该数组中的某些字符串是否包含搜索文本。@如果您搜索特定项,SHINTOJOSEPH接受的答案将返回内部数组sectionObjects中的所有对象,或者你想要这样的东西,如果你在TextField afg中输入,那么它应该只返回两个对象“阿富汗牧羊犬”,“阿富汗猎犬”和A部分,我问这个问题是因为接受的答案将返回sectionObjects数组中的所有对象。可能与另一个重复:。它们使用数组的我使用的结构,因此有些混淆
$0。sectionObjects
是数组,您必须检查该数组中的某些字符串是否包含搜索文本。@如果您搜索特定项,SHINTOJOSEPH Accepted answer将返回内部数组sectionObjects中的所有对象,或者你想要类似的东西,如果你输入textfield afg,那么它应该只返回两个对象“阿富汗牧羊犬”,“阿富汗猎犬”和A部分,我问这个问题是因为接受的答案将返回sectionObjects数组中的所有对象。Welcome mate:)在搜索时,会话的name字段中会出现name,但是section的所有id和图像都会出现在filteredsection对象中,该id和图像来自于该id和图像所在的位置。您的问题中没有类似的内容,如果struct对象包含section_id和sectionoimagesStruct,您所做的不正确,您需要做的是创建另一个struct并使用属性对象id生成,ph和image所有类型的字符串,然后在对象结构中创建此结构的数组Welcome mate:)在搜索时,会话的名称字段中会出现名称,但该节的所有id和图像都会出现在filteredsection对象中,此id和图像来自该对象的位置问题中没有类似的内容如果struct对象包含section_id和sectionoimagesStruct,您所做的是创建另一个结构,并使用property object、id、ph和image创建所有类型的字符串,然后在对象结构中创建此结构的数组