gpt4 book ai didi

iOS - 搜索栏,错误

转载 作者:行者123 更新时间:2023-11-28 12:28:50 25 4
gpt4 key购买 nike

我试图在 tableView 中实现一个 searchBar,但我收到了这个错误。我不知道如何删除它。

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchingDataArray = origanalArray.filter({ (text) -> Bool in
let tmp: String = text
let range = tmp.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
return range.location != NSNotFound
})
if (searchingDataArray.count == 0) {
searching = false
} else {
searching = true
}
self.tableView.reloadData()
}

这个错误出现在代码的第 5 行,听起来像这样:Value of type range string.index has no member location

最佳答案

Updated for Swift 3:

使用代码行;

// MARK:- Varirabl Declartion

@IBOutlet weak var mSearchBar: UISearchBar!
@IBOutlet weak var mTableView: UITableView!

var isSearch : Bool = false
var arrCountry = ["Afghanistan", "Algeria", "Bahrain","Brazil", "Cuba", "Denmark","Denmark", "Georgia", "Hong Kong", "Iceland", "India", "Japan", "Kuwait", "Nepal"]
var arrFilter:[String] = []

//MARK: UISearchBar 委托(delegate)方法

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

if searchText.characters.count == 0 {
isSearch = false;
self.mTableView.reloadData()
} else {
arrFilter = arrCountry.filter({ (text) -> Bool in
let tmp: NSString = text as NSString
let range = tmp.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
return range.location != NSNotFound
})
if(arrFilter.count == 0){
isSearch = false;
} else {
isSearch = true;
}
self.mTableView.reloadData()
}
}

关于iOS - 搜索栏,错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42646086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com