swift – viewDidLoad中之前和之后的区别

前端之家收集整理的这篇文章主要介绍了swift – viewDidLoad中之前和之后的区别前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图将searchBar设置为viewDidLoad中的tableHeaderView:
  1. override func viewDidLoad() {
  2. super.viewDidLoad()
  3.  
  4.  
  5. // SearchController initializiation
  6. self.searchController = UISearchController.init(searchResultsController: nil)
  7. self.searchController.delegate = self
  8. self.searchController.searchBar.delegate = self
  9. self.searchController.searchBar.sizeToFit()
  10. self.searchController.searchResultsUpdater = self
  11. self.searchController.searchBar.barTintColor = UIColor.white
  12. self.searchController.searchBar.keyboardAppearance = .default
  13. self.searchController.searchBar.backgroundColor = UIColor.white
  14. self.searchController.hidesNavigationBarDuringPresentation = true
  15. self.searchController.obscuresBackgroundDuringPresentation = false
  16.  
  17.  
  18. self.tableView.tableHeaderView = self.searchController.searchBar
  19. self.definesPresentationContext = true
  20.  
  21. self.fetch()
  22. self.tableView.reloadData()
  23.  
  24. }

这个我的fetch()函数

  1. func fetch() {
  2. let fetchRequest:NSFetchRequest<Phone> = Phone.fetchRequest()
  3. fetchRequest.sortDescriptors = [NSSortDescriptor.init(key: "header",ascending: true),NSSortDescriptor.init(key: "date",ascending: true)]
  4.  
  5. self.fetchedResultsController = NSFetchedResultsController.init(fetchRequest: fetchRequest,managedObjectContext: self.managedObjectContext,sectionNameKeyPath: "header",cacheName: nil)
  6. self.fetchedResultsController.delegate = self
  7.  
  8. do {
  9. try self.fetchedResultsController.performFetch()
  10. self.tableView.reloadData()
  11. } catch {}
  12.  
  13. }

但我不明白他们是不会工作的. Xcode崩溃,实际上没有发生.然后我尝试改变viewDidLoad里面的内容

  1. override func viewDidLoad() {
  2. super.viewDidLoad()
  3.  
  4. self.fetch()
  5. self.tableView.reloadData()
  6.  
  7. // SearchController initializiation
  8. self.searchController = UISearchController.init(searchResultsController: nil)
  9. self.searchController.delegate = self
  10. self.searchController.searchBar.delegate = self
  11. self.searchController.searchBar.sizeToFit()
  12. self.searchController.searchResultsUpdater = self
  13. self.searchController.searchBar.barTintColor = UIColor.white
  14. self.searchController.searchBar.keyboardAppearance = .default
  15. self.searchController.searchBar.backgroundColor = UIColor.white
  16. self.searchController.hidesNavigationBarDuringPresentation = true
  17. self.searchController.obscuresBackgroundDuringPresentation = false
  18.  
  19.  
  20. self.tableView.tableHeaderView = self.searchController.searchBar
  21. self.definesPresentationContext = true
  22.  
  23.  
  24.  
  25. }

成功!工作正常我不明白有什么区别?

这是因为您设置了searchResultsUpdater和searchBar的代理,甚至在您的fetchedResultsController被初始化之前.你的代码searchResultsUpdater或searchBar的代理方法中的某些地方访问你的fetchedResultsController并且得到它,所以你的应用程序崩溃.我的建议是避免崩溃是使searchResultsController作为计算或惰性属性.

猜你在找的Swift相关文章