ios 类没有初始值设定项:Swift 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32718428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class has no initializers: Swift Error
提问by iGermanos
I have a problem with my ViewController.
我的 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.
问题是什么?错误是该类没有初始化程序。我没有没有价值的变量。
回答by Sahil Kapoor
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:
或者如果您没有在视图生命周期中对其进行初始化,请使用 optional 来避免崩溃:
var searchController: UISearchController?
回答by jlngdt
Your line which catch the error is:
您捕获错误的行是:
var searchController: UISearchController
var searchController: UISearchController
because you never init searchController in a LifeCycle init functionfrom your UIViewController. I advice you not to force unwrapthe var (like Sahil said above) but to properly init it into an init func like this:
因为你从来没有在UIViewController的LifeCycle init 函数中初始化 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
}
In Swift, you always should avoid force unwrap Object like above, to avoid crash in your app, or use if-Let/Guard-Let template
在 Swift 中,你总是应该避免像上面那样强制解包 Object,以避免在你的应用程序中崩溃,或者使用 if-Let/Guard-Let 模板
Cheers from France
来自法国的欢呼