Swift/XCode 6.4:来自 UITableViewController 的 class 没有初始化器
Swift/XCode 6.4: class from UITableViewController has no initializers
我有一个 class 作为 UITableViewController 的扩展:
import UIKit
class DetailsMyTasksViewController: UITableViewController {
@IBOutlet var backButton : UIButton!
var detailItem: Task
@IBAction func backToHome(sender : AnyObject) {
/*dispatch_async(dispatch_get_main_queue()){
self.performSegueWithIdentifier("selectedTasksToHome", sender: self)
}*/
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 11 /* number of Task information/attributes --> 11 rows */
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("SelectedMyTaskDetails", forIndexPath: indexPath) as! UITableViewCell
switch indexPath.row {
case 0:
cell.textLabel?.text = "Task id"
cell.detailTextLabel?.text = String(self.detailItem.id)
break
case 1:
cell.textLabel?.text = "Titolo"
cell.detailTextLabel?.text = self.detailItem.titolo
break
case 2:
cell.textLabel?.text = "Oggetto"
cell.detailTextLabel?.text = self.detailItem.oggetto
break
case 3:
cell.textLabel?.text = "Check mail abilitato"
if (self.detailItem.check_mail) {
cell.detailTextLabel?.text = "Abilitato"
}
else {
cell.detailTextLabel?.text = "Non abilitato"
}
break
case 4:
cell.textLabel?.text = "Progetto id"
cell.detailTextLabel?.text = String(self.detailItem.id_progetto)
break
case 5:
cell.textLabel?.text = "Progetto nome"
cell.detailTextLabel?.text = self.detailItem.progetto_nome
break
case 6:
cell.textLabel?.text = "Assegnato a"
cell.detailTextLabel?.text = self.detailItem.assegnato_a
break
case 7:
cell.textLabel?.text = "Richiesto da"
cell.detailTextLabel?.text = self.detailItem.richiesto_da
break
case 8:
cell.textLabel?.text = "Priorità"
cell.detailTextLabel?.text = self.detailItem.priorita
break
case 9:
cell.textLabel?.text = "Termine consegna"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-mm-dd"
cell.detailTextLabel?.text = dateFormatter.stringFromDate(self.detailItem.termine_consegna)
break
case 10:
cell.textLabel?.text = "Stato"
cell.detailTextLabel?.text = self.detailItem.stato
break
default:
break
}
return cell
}
}
我不知道问题出在哪里,因为我尝试编译时出现标题错误。此外,我有另一个 class 类似于另一个 table 视图控制器,它工作得很好,而且 class 没有初始化程序。有什么问题吗?
更新
我不知道为什么,但我只是改变了
var detailItem: Task
和
var detailItem: Task?
并且有效。那是什么?
如果您设置 var detailItem,您说它属于任务类型。没有'? detailItem 不能为 nil,所以它必须有一个值。但是用代码
var detailItem: Task
你没有给它赋值(它是空的,这是被禁止的)。但是如果你为你的 class 设置了一个初始值设定项,你设置了 detailItem 的值,DetailsMyTasksViewController 的对象将永远不会有一个没有值的 detailItem,这是可能的。
通过添加“?”对于 Task 你说 detailItem 可以是空的(nil)所以它默认设置为 nil。
所以最后 xcode 并没有告诉你错误是什么,而是告诉你你可能想做些什么来修复它。
结论:
- 错误:你说 detailItem 不能为 nil 但你没有给它一个值。
-1。解决方案:添加一个'?'说它可以是 nil
-2。解决方案:在 class
中添加一个初始值设定项
-3。解决方案:做类似的事情:
var detailItem: Task = Task(...)
我有一个 class 作为 UITableViewController 的扩展:
import UIKit
class DetailsMyTasksViewController: UITableViewController {
@IBOutlet var backButton : UIButton!
var detailItem: Task
@IBAction func backToHome(sender : AnyObject) {
/*dispatch_async(dispatch_get_main_queue()){
self.performSegueWithIdentifier("selectedTasksToHome", sender: self)
}*/
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 11 /* number of Task information/attributes --> 11 rows */
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("SelectedMyTaskDetails", forIndexPath: indexPath) as! UITableViewCell
switch indexPath.row {
case 0:
cell.textLabel?.text = "Task id"
cell.detailTextLabel?.text = String(self.detailItem.id)
break
case 1:
cell.textLabel?.text = "Titolo"
cell.detailTextLabel?.text = self.detailItem.titolo
break
case 2:
cell.textLabel?.text = "Oggetto"
cell.detailTextLabel?.text = self.detailItem.oggetto
break
case 3:
cell.textLabel?.text = "Check mail abilitato"
if (self.detailItem.check_mail) {
cell.detailTextLabel?.text = "Abilitato"
}
else {
cell.detailTextLabel?.text = "Non abilitato"
}
break
case 4:
cell.textLabel?.text = "Progetto id"
cell.detailTextLabel?.text = String(self.detailItem.id_progetto)
break
case 5:
cell.textLabel?.text = "Progetto nome"
cell.detailTextLabel?.text = self.detailItem.progetto_nome
break
case 6:
cell.textLabel?.text = "Assegnato a"
cell.detailTextLabel?.text = self.detailItem.assegnato_a
break
case 7:
cell.textLabel?.text = "Richiesto da"
cell.detailTextLabel?.text = self.detailItem.richiesto_da
break
case 8:
cell.textLabel?.text = "Priorità"
cell.detailTextLabel?.text = self.detailItem.priorita
break
case 9:
cell.textLabel?.text = "Termine consegna"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-mm-dd"
cell.detailTextLabel?.text = dateFormatter.stringFromDate(self.detailItem.termine_consegna)
break
case 10:
cell.textLabel?.text = "Stato"
cell.detailTextLabel?.text = self.detailItem.stato
break
default:
break
}
return cell
}
}
我不知道问题出在哪里,因为我尝试编译时出现标题错误。此外,我有另一个 class 类似于另一个 table 视图控制器,它工作得很好,而且 class 没有初始化程序。有什么问题吗?
更新 我不知道为什么,但我只是改变了
var detailItem: Task
和
var detailItem: Task?
并且有效。那是什么?
如果您设置 var detailItem,您说它属于任务类型。没有'? detailItem 不能为 nil,所以它必须有一个值。但是用代码
var detailItem: Task
你没有给它赋值(它是空的,这是被禁止的)。但是如果你为你的 class 设置了一个初始值设定项,你设置了 detailItem 的值,DetailsMyTasksViewController 的对象将永远不会有一个没有值的 detailItem,这是可能的。
通过添加“?”对于 Task 你说 detailItem 可以是空的(nil)所以它默认设置为 nil。
所以最后 xcode 并没有告诉你错误是什么,而是告诉你你可能想做些什么来修复它。
结论:
- 错误:你说 detailItem 不能为 nil 但你没有给它一个值。
-1。解决方案:添加一个'?'说它可以是 nil
-2。解决方案:在 class
中添加一个初始值设定项
-3。解决方案:做类似的事情:
var detailItem: Task = Task(...)