任何人都可以告诉我为什么我的过滤后的数组是空的吗?

时间:2017-07-07 16:34:10

标签: swift uitableview swift3 uisearchbar searchbar

我有一个表格视图和一个搜索栏(以编程方式)。我想显示过滤结果,所以我创建了一个数组(称为:todoTitle),其中包含我的待办事项活动的标题(我有一个单独的对象,其中一个属性是标题)。我使用updateSearchResults方法,在其中我使用filter方法返回正确的待办事项。为了检查数组是否为空,我在该函数内部编写代码以打印每个数组内的待办事项。这是我在updateSearchResults(for :)函数

中的代码
    //filtering through todos' titles
    filteredTodos = self.todosTitle.filter({ (title: String) -> Bool in
        if title.lowercased().contains((self.searchController.searchBar.text?.lowercased())!) {
            return true
        } else {
            print ("S \(todosTitle)")
            print ("t \(title)")
            print (filteredTodos)

            return false

        }
    })
    //updating the results TableView
    self.resultsController.tableView.reloadData()

}

``

todosTitle数组不为空,所以我不明白为什么我的filteredTodos为空。有没有人有任何想法为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

您只需要检查待办事项标题是否包含搜索文本。这是我用字符串数组和搜索文本字符串测试的简化示例。也许将你的搜索文本分开,以确保你从searchBar.text获得你期望的字符串

let todosTitle = ["One", "Two", "Three"]
let searchText = "t"

//filtering through todos' titles
let filteredTodos = todosTitle.filter({ (title: String) -> Bool in
    return title.lowercased().contains(searchText.lowercased())
})

print(filteredTodos)  //["Two", "Three"]
相关问题