2016-10-11 53 views
0

我在編程方面很新,我試圖將這個如果其他結構轉換成Swift語言的切換案例,我感謝您的幫助,謝謝。轉換如果其他結構切換到SWIFT案例

這裏是我的代碼

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if (segue.identifier == "ShowDefinition") { 
     if let destinationViewController = segue.destinationViewController as? EnglishViewController { 
      if let definition = sender as? String { 
       if definition == "Abstraction" { 
        destinationViewController.titleMsg = "Abstraction" 
        destinationViewController.definitionMsg = "definition 1" 
       } else if definition == "Binary System" { 
        destinationViewController.titleMsg = "Binary System" 
        destinationViewController.definitionMsg = "definition 2" 
       } else if definition == "Computer" { 
        destinationViewController.titleMsg = "Computer" 
        destinationViewController.definitionMsg = "definition 3" 

       } else if definition == "Internet" { 
        destinationViewController.titleMsg = "Internet" 
        destinationViewController.definitionMsg = "definition 4" 

       } else if definition == "Virtual Reality" { 
        destinationViewController.titleMsg = "Virtual Reality" 
        destinationViewController.definitionMsg = "definition 5" 

       } 
      } 
     } 
    } 
} 

回答

1

事情是這樣的:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if (segue.identifier == "ShowDefinition") { 
     if let destinationViewController = segue.destinationViewController as? EnglishViewController { 
      if let definition = sender as? String { 
       switch definition { 
       case "Abstraction": 
        destinationViewController.titleMsg = "Abstraction" 
        destinationViewController.definitionMsg = "definition 1" 
       case "Binary System": 
        destinationViewController.titleMsg = "Binary System" 
        destinationViewController.definitionMsg = "definition 2" 
       case "Computer": 
        destinationViewController.titleMsg = "Computer" 
        destinationViewController.definitionMsg = "definition 3" 
       case "Internet": 
        destinationViewController.titleMsg = "Internet" 
        destinationViewController.definitionMsg = "definition 4" 
        destinationViewController.titleMsg = "Abstraction" 
        destinationViewController.definitionMsg = "definition 1" 
       case "Virtual Reality": 
        destinationViewController.titleMsg = "Virtual Reality" 
        destinationViewController.definitionMsg = "definition 5" 
       default: 
        destinationViewController.titleMsg = "" 
        destinationViewController.definitionMsg = "" 
       } 
      } 
     } 
    } 
} 

檢查要與default值做什麼,那會如果沒有case比賽被擊中。

+0

非常感謝你! –

0

使用此代碼

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if (segue.identifier == "ShowDefinition") { 
     if let destinationViewController = segue.destinationViewController as? EnglishViewController { 
      if let definition = sender as? String { 
       switch definition { 
       case "Abstraction": 
        destinationViewController.titleMsg = "Abstraction" 
        destinationViewController.definitionMsg = "definition 1" 
       case "Binary System": 
        destinationViewController.titleMsg = "Binary System" 
        destinationViewController.definitionMsg = "definition 2" 
       case "Computer": 
        destinationViewController.titleMsg = "Computer" 
        destinationViewController.definitionMsg = "definition 3" 
       case "Internet": 
        destinationViewController.titleMsg = "Internet" 
        destinationViewController.definitionMsg = "definition 4" 
       case "Virtual Reality": 
        destinationViewController.titleMsg = "Virtual Reality" 
        destinationViewController.definitionMsg = "definition 5" 
       default: 
        // As per your requirement 
        break 
       } 
      } 
     } 
    } 
} 
1

由於​​將包含總是definition字符串就可以使代碼更短

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "ShowDefinition" { 
    if let destinationViewController = segue.destinationViewController as? EnglishViewController { 
     if let definition = sender as? String { 
     switch definition { 
     case "Abstraction": 
      destinationViewController.definitionMsg = "definition 1" 
     case "Binary System": 
      destinationViewController.definitionMsg = "definition 2" 
     case "Computer": 
      destinationViewController.definitionMsg = "definition 3" 
     case "Internet": 
      destinationViewController.definitionMsg = "definition 4" 
     case "Virtual Reality": 
      destinationViewController.definitionMsg = "definition 5" 
     default : 
      destinationViewController.definitionMsg = "n/a" 

     } 
     destinationViewController.titleMsg = definition 
     } 
    } 
    } 
} 

你可以再前進一步,宣佈案件

枚舉
enum Definitions : String { 
    case Abstraction, BinarySystem = "Binary System", Computer, Internet, VirtualReality = "Virtual Reality" 
} 

這節省了default聲明,因爲案件是詳盡的。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if (segue.identifier == "ShowDefinition") { 
    if let destinationViewController = segue.destinationViewController as? EnglishViewController { 
     if let definition = sender as? String, let definitionEnum = Definitions(rawValue:definition) { 
     switch definitionEnum { 
     case .Abstraction: 
      destinationViewController.definitionMsg = "definition 1" 
     case .BinarySystem: 
      destinationViewController.definitionMsg = "definition 2" 
     case .Computer: 
      destinationViewController.definitionMsg = "definition 3" 
     case .Internet: 
      destinationViewController.definitionMsg = "definition 4" 
     case .VirtualReality: 
      destinationViewController.definitionMsg = "definition 5" 
     } 
     destinationViewController.titleMsg = definition 
     } 
    } 
    } 
} 
0

在這個特定的使用情況下,你不必真的訴諸串行if-elseswitch塊,作爲供試品僅適用於測試給定String實例(definition)是否等於的任何成員給出String的列表。

由於destinationViewController.titleMsgdefinition的值相同(在匹配的情況下),並且destinationViewController.definitionMsg只是一個運行號碼w.r.t.的String的列表中的索引(+1),您可以捕獲在一個可選的綁定條款的所有邏輯:

let validTitles = ["Abstraction", "Binary System", "Computer", "Internet", "Virtual Reality"] 
let definition = "Abstraction" 

if let definitionAndTitle = validTitles.enumerated().filter({$1 == definition}).first { 
    print("titleMsg=\(definitionAndTitle.1)") 
    print("definitionMsg=\(definitionAndTitle.0 + 1)") 
} 

適用於你的例子(簡單地擴展現有的可選綁定條款)

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if (segue.identifier == "ShowDefinition") { 
     if let destinationViewController = segue.destinationViewController as? EnglishViewController { 
      // or, better, keep this list of titles as e.g. a static 
      // immutable property of your viewcontroller class, as these 
      // should already be set at compile time. 
      let validTitles = ["Abstraction", "Binary System", 
           "Computer", "Internet", "Virtual Reality"] 
      if let definition = sender as? String, let defNum = 
       validTitles.enumerated().filter({$1 == definition}).first?.0 { 
       destinationViewController.titleMsg = definition 
       destinationViewController.definitionMsg = "definition \(defNum+1)" 
      } 
     } 
    } 
}