2015-09-04 66 views
1

我有一个类为的UITableViewController的扩展:斯威夫特/ 6.4的XCode:从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 
} 

} 

我不知道问题出在哪里,因为我得到了错误的标题时,我尝试编译。此外,我有另一个类似于这个到另一个表视图控制器,它的作品完美,也在该类没有初始化。什么问题没有?

UPDATE 我不知道为什么,但我只是改变

var detailItem: Task 

var detailItem: Task? 

和它的作品。那是什么?

回答

1

如果您设置var detailItem,则表示它属于Task类型。没有'?' detailItem不能为零,所以它必须有一个值。但随着代码

var detailItem: Task 

你不给它一个值(它是空的,这是禁止的)。但是如果你设置了一个初始化器到你设置detailItem值的类中,那么DetailsMyTasksViewController的对象永远不会有一个没有值的detailItem,这是可能的。

通过添加'?'任务你说detailItem可以是空的(零),所以它默认设置为零。

因此,最终xcode不会告诉你错误是什么,它告诉你你可能想要解决它。

对于结论:
错误:你说detailItem不能为零,但你不给它一个值。
-1。解决方案:添加一个'?'说它可以不是
-2。解决方案:为您的课程添加初始化程序
-3。解决办法:做一些类似的事情:

​​
+0

谢谢你队友,这对我更好理解是非常有启发性和有用的。谢谢你的心:) –

+0

没问题我记得我第一次看到这个错误,并且不是真的了解它;) –