2017-06-19 53 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 ?? "") == "" 

,但我個人更喜歡將它寫入即使你確定它不是零,也不要強行展開,所以我會推薦第一個。