类没有初始化器:Swift错误

17

我有一个与我的ViewController有关的问题。

我的代码在初始化器方面存在错误,我无法理解其中的原因。

请花点时间查看我的代码:

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
    }

}

问题是什么?错误在于该类没有初始化程序。我没有没有值的变量。


确切的错误信息是什么? - KornMuffin
2个回答

35

有问题的行是

var searchController: UISearchController

改为

var searchController: UISearchController!

如果您不在视图生命周期中初始化它,请使用可选项以避免崩溃:

var searchController: UISearchController?

4
也许把它写成 "var searchController: UISearchController?" 会更好,这样如果为空就不会导致应用崩溃,而且还能获得默认的初始化值 nil。 - Korpel
好的,已经添加到答案中了。谢谢 :) - Sahil Kapoor

2
您的代码中捕获错误的行是:
```var searchController: UISearchController```
这是因为您从UIViewController中的生命周期初始化函数中没有初始化searchController。我建议您不要强制展开变量(就像Sahil在上面所说的那样),而是像这样适当地在一个init函数中进行初始化:
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
}

在Swift中,您应该避免像上面那样强制解包对象,以避免应用程序崩溃,或使用if-Let / Guard-Let模板。

来自法国的祝福


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接