2016-02-17 37 views
-1

我已經盡我所能來轉換此代碼,但仍有一些錯誤。有些人可以幫忙,也可以解釋哪些Obj-C代碼與Swift代碼相關?謝謝。Obj-C函數轉換爲Swift 2函數

對象 -

//NTRMainViewConroller.m 
- (IBAction)unwindFromModalViewController:(UIStoryboardSegue *)segue 
{ 
    if ([segue.sourceViewController isKindOfClass:[NTRColorsTableViewController class]]) { 
     NTRColorsTableViewController *colorsViewConroller = segue.sourceViewController; 
     // if the user clicked Cancel, we don't want to change the color 
     if (colorsViewConroller.selectedColor) { 
      self.view.backgroundColor = colorsViewConroller.selectedColor; 
     } 
    } 
} 

我試圖translateSwift

var colorsViewController: ColorsTableViewController 
    @IBAction func unwindFromModalViewController(segue: UIStoryboardSegue) { 
     if segue.sourceViewController.isKindOfClass(ColorsTableViewController class) { 
      colorsViewController = segue.sourceViewController 
      if(colorsViewController.selected) 
     } 

    } 
+0

我可以假設'var colorsViewController:ColorsTableViewController'不屬於示例嗎? – atomkirk

+0

'if(colorsViewController.selected)'然後...什麼? :) –

+0

什麼是編譯器錯誤? – atomkirk

回答

2

我會做這樣的事情:

@IBAction func unwindFromModalViewController(segue: UIStoryboardSegue) { 
    guard let colorsViewController = segue.sourceViewController as? ColorsTableViewController else { return } 
    if colorsViewController.selected ... 
} 

現在,如果你有更多的條件之後,我會推薦g oing:

@IBAction func unwindFromModalViewController(segue: UIStoryboardSegue) { 
    if let viewController = segue.sourceViewController as? ColorsTableViewController else { 
     if viewController.selected ... 
    } else ... 
}