2017-06-19 33 views
0

我的应用程序崩溃时,在搜索栏中的文本与错误:thread1:信号SIGABRT可能是问题updateSearchResults()方法? 或数组的类型?我的初学者很快就有任何想法?搜索和过滤数组firebase数据swift3

@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() 
} 

回答

0
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 ?? "") == "" 

,但我个人更喜欢将它写入即使你确定它不是零,也不要强行展开,所以我会推荐第一个。