【问题标题】:Class has no initializers: Swift Error类没有初始化器:Swift 错误
【发布时间】:2015-09-22 13:37:22
【问题描述】:

我的 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
    }

}

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

【问题讨论】:

  • 确切的错误信息是什么?

标签: ios swift viewcontroller


【解决方案1】:

问题线是

var searchController: UISearchController

改成

var searchController: UISearchController!

或者如果您没有在视图生命周期中对其进行初始化,请使用 optional 以避免崩溃:

var searchController: UISearchController?

【讨论】:

  • 也许最好是“var searchController: UISearchController?”相反,如果它为零,则不会使应用程序崩溃。加上它将获得 nil 的默认初始化程序
  • 是的,添加了这个来回答。谢谢:)
【解决方案2】:

您发现错误的行是:

var searchController: UISearchController

因为您从未在 UIViewController 的 LifeCycle 初始化函数 中初始化 searchController。我建议您不要强制解包 var(就像上面的 Sahil 所说),而是正确地将其初始化为这样的 init func:

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 中,你总是应该避免像上面那样强制解包 Object,以避免在你的应用程序中崩溃,或者使用 if-Let/Guard-Let 模板

来自法国的欢呼

【讨论】: