类没有初始值设定项:Swift Error

时间:2015-09-22 13:37:22

标签: ios swift viewcontroller

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

}

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

2 个答案:

答案 0 :(得分:34)

问题线是

var searchController: UISearchController

将其更改为

var searchController: UISearchController!

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

var searchController: UISearchController?

答案 1 :(得分:2)

发现错误的行是:

var searchController: UISearchController

因为你永远不会从你的UIViewController中的 LifeCycle初始化函数中初始化searchController。我建议你不要强行打开 var(比如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模板

来自法国的欢呼声