代码之家  ›  专栏  ›  技术社区  ›  128K

使用flatmap实现搜索功能

  •  1
  • 128K  · 技术社区  · 7 年前

    var sections = [
    
            Section(sec: "One",
                    subSec: ["A", "B", "C", "D", "E"],
                    expanded: false),
    
            Section(sec: "Two",
                    subSec: ["A", "B", "C", "D", "E"],
                    expanded: false),
    
            Section(sec: "Three",
                    subSec: ["A", "B", "C", "D", "E"],
                    expanded: false),
    

    struct Section {
        var sec: String!
        var subSec: [String]! // [ ] = Array of Strings
        var expanded: Bool!
    
        init(sec: String, subSec: [String], expanded: Bool) {
            self.sec = sec
            self.subSec = subSec
            self.expanded = expanded
        }
    }
    

    我正在尝试建立一个搜索功能。它适用于标准数组,但不适用于我的变量,因为它更复杂。 我(在帮助下)使用flatmap调整了代码,但我不知道如何将其映射到子字符串。

    func filterContentForSearchText(_ searchText: String, scope: String = "All") {
            filtered = sections.flatMap{ return $0.subSec.lowercased().contains(searchText.lowercased()) ? $0.subSec : nil }
    
            tableView.reloadData()
        }
    

    我当前遇到的错误是: Value type [String] has no value lowercased

    附加代码:

    var filtered = [Section]()
    let searchController = UISearchController(searchResultsController: nil)
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   Adrian Bobrowski    7 年前

    baseText.range(
        of: searchText, options: .caseInsensitive, range: nil, locale: nil
    ) != nil
    

    如果你想得到整个部分,那么你不需要使用 flatMap filter

    var filtered: [Section] = []
    
    func filterContentForSearchText(_ searchText: String, scope: String = "All") {
        filtered = sections.filter { section in
            section.subSec.contains { 
                $0.range(
                    of: searchText, options: .caseInsensitive, range: nil, locale: nil
                ) != nil 
            }
        }
    }
    

    但是,如果在该部分中,您只需要包含搜索文本的子部分,那么

    var filtered: [Section] = []
    
    func filterContentForSearchText(_ searchText: String, scope: String = "All") {
        filtered = sections.map { section in
            var section = section
            section.subSec = section.subSec.filter {
                $0.range(
                    of: searchText, options: .caseInsensitive, range: nil, locale: nil
                ) != nil
            }
            return section
        }
    }
    

    var filtered: [String] = []
    
    func filterContentForSearchText(_ searchText: String, scope: String = "All") {
        filtered = sections.flatMap { section in
            section.subSec.filter {
                $0.range(
                    of: searchText, options: .caseInsensitive, range: nil, locale: nil
                ) != nil
            }
        }
    }
    
        2
  •  0
  •   Community THelper    4 年前

    您的错误可以通过以下方式解决: map

    [[String]] 而不是 [String]

    sections 所以我得到一个1D数组,其中所有子部分都是字符串

    要做第一件事,你需要 flatMap filter .

    let filtered: [String] = 
        sections.flatMap { $0.subSec }
                 .filter { $0.lowercased().contains(searchText.lowercased()) }
    
        3
  •  0
  •   algrid    7 年前

    $0.subSec.lowercased().contains(searchText.lowercased())
    

    你可以试试

    $0.subSec.contains({$0.lowercased() == searchText.lowercased()})