Class has no initializers: Swift Error

21,779

Solution 1

Problematic line is

var searchController: UISearchController

Change it to

var searchController: UISearchController!

or if you are not initializing it in view life cycles, use optional to avoid crashes:

var searchController: UISearchController?

Solution 2

Your line which catch the error is:

var searchController: UISearchController

because you never init searchController in a LifeCycle init function from your UIViewController. I advice you not to force unwrap the var (like Sahil said above) but to properly init it into an init func like this:

override init(frame: CGRect) {
    super.init(frame: frame)

    setUp()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    setUp()
}

func setUp() {
    searchController = UISearchController() //Or any init you can use to perform some custom initialization
}

In Swift, you always should avoid force unwrap Object like above, to avoid crash in your app, or use if-Let/Guard-Let template

Cheers from France

Share:
21,779
iGermanos
Author by

iGermanos

Updated on July 28, 2022

Comments

  • iGermanos
    iGermanos almost 2 years

    I have a problem with my ViewController.

    My code has an error about initializers and I can't understand why.

    Please, take a moment to look at my code:

    import UIKit
    
    class ViewController: UIViewController, UITableViewDataSource {
    
    let sectionsTableIdentifier = "SectionsTableIdentifier"
    
    var names: [String: [String]]!
    var keys: [String]!
    
    @IBOutlet weak var tableView: UITableView!
    
    var searchController: UISearchController
    
    //methods
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: sectionsTableIdentifier)
    
        let path = NSBundle.mainBundle().pathForResource("sortednames", ofType: "plist")
    
        let namesDict = NSDictionary(contentsOfFile: path!)
        names = namesDict as! [String: [String]]
        keys = namesDict!.allKeys as! [String]
        keys = keys.sort()
    
        let resultsController = SearchResultsController()
    
        resultsController.names = names
        resultsController.keys = keys
        searchController = UISearchController(searchResultsController: resultsController)
    
        let searchBar = searchController.searchBar
        searchBar.scopeButtonTitles = ["All", "Short", "Long"]
        searchBar.placeholder = "Enter a search term"
    
        searchBar.sizeToFit()
        tableView.tableHeaderView = searchBar
        searchController.searchResultsUpdater = resultsController
    
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return keys.count
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let key = keys[section]
        let nameSection = names[key]!
        return nameSection.count
    }
    
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return keys[section]
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(sectionsTableIdentifier, forIndexPath: indexPath) as UITableViewCell
    
        let key = keys[indexPath.section]
        let nameSection = names[key]!
        cell.textLabel!.text = nameSection[indexPath.row]
    
        return cell
    }
    
    func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
    
    
           return keys
        }
    
    }
    

    What is the problem? The error is that the class has no initializer. I have no variables with no value.

  • Korpel
    Korpel over 8 years
    perhaps it would be better to be "var searchController: UISearchController?" instead so if it is nil there won't crash the application. plus it will get the default initialiser of nil
  • Sahil Kapoor
    Sahil Kapoor over 8 years
    Yeah, added that to answer. Thanks :)