gpt4 book ai didi

ios - 搜索和过滤数组firebase数据swift3

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

我的应用程序在搜索栏中搜索文本时崩溃,并出现错误:thread1:signal SIGABRT 可能是 updateSearchResults() 方法有问题?或数组类型?我是 Swift 的初学者,有什么想法吗?

@IBOutlet weak var tableView: UITableView!

var data = [Any]()
var ref:FIRDatabaseReference!

// Filter Data from Firebase
var filteredData = [Any]()

// Declare searchBar

let searchController = UISearchController(searchResultsController: nil)


//is the device landscape or portrait
var isPortraid = true

@IBOutlet weak var bannerView: GADBannerView!



func fetchDataFromFirebase(){
EZLoadingActivity.show("caricamento...", disableUI: true)
ref = FIRDatabase.database().reference()
ref.observe(.value, with: { (snapshot) in
let dataDict = snapshot.value as! NSDictionary
self.data = dataDict["data"] as! [Any]
self.filteredData = self.data
print ("Sacco di merda:\(self.filteredData)")
self.tableView.reloadData()
EZLoadingActivity.hide()
})
}



override func viewDidLoad() {
super.viewDidLoad()


tableView.delegate = self
fetchDataFromFirebase()

// Implement searchBar
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar




NotificationCenter.default.addObserver(self, selector: #selector(MainViewController.orientationChanged), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)


}


//TableView Data Source and Delegate


func numberOfSections(in tableView: UITableView) -> Int {
return 1
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return filteredData.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "MainCell", for:indexPath) as! MainScreenTableViewCell
let rowData = self.filteredData[indexPath.row] as! NSDictionary
let imageName = rowData["imageName"] as! String
cell.backgroundImageView.image = UIImage(named: imageName)

let label = rowData["categoryName"] as! String
cell.mealCategoryLabel.text = label
return cell
}



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let categoryViewController = storyboard.instantiateViewController(withIdentifier: "CategoryViewController") as! CategoryViewController
let rowData = self.data[indexPath.row] as! NSDictionary
categoryViewController.categoryTitle = rowData["categoryName"] as! String
let categoryData = rowData["category"] as! [Any]
categoryViewController.data = categoryData
self.navigationController?.pushViewController(categoryViewController, animated: true)

}





func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if isPortraid {
return UIScreen.main.bounds.height/3
} else {
return UIScreen.main.bounds.height/1.2
}
}

//更新搜索方法

func updateSearchResults(for searchController: UISearchController) {
if searchController.searchBar.text! == ""{
filteredData = data
} else {
filteredData = data.filter{($0 as AnyObject).contains(searchController.searchBar.text!)}

}
self.tableView.reloadData()
}

最佳答案

if searchController.searchBar.text! == ""

这几乎肯定是罪犯。 UI 对象上的 text 属性在为空时通常为零,因此当您强制解开它时,您的应用程序会崩溃。你不应该永远强行打开某些东西,除非你绝对确定它在那时永远不会为零。

有几种不同的方法可以处理这个问题,基本上就是在对它进行任何操作之前确保 text 不为零。

我个人会重写 if 语句来解开非空情况的可选内容:

if let text = searchController.searchBar.text, text != "" {
filteredData = data.filter{($0 as AnyObject).contains(text)}
} else {
filteredData = data
}

您还可以使用 nil-coalescing :

if (searchController.searchBar.text ?? "") == ""

但就我个人而言,我更喜欢编写它以避免强制展开,即使您确定它不为零,所以我会推荐第一个。

关于ios - 搜索和过滤数组firebase数据swift3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44640053/

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