![](/img/trans.png)
[英]Unable to assign different variable types according to condition in same class in Swift
[英]Swift 3: How to assign variable of different option types without multiple if let statements?
首先,如果標題不明確,我深表歉意。 請隨意更改它以適合問題描述。
我將盡力描述我的問題,但首先這是一段代碼:
if let vc = currentVC as? FirstViewController
{
vc.doSameMethod()
}
else if let vc = currentVC as? SecondViewController
{
vc.doSameMethod()
}
else if let vc = currentVC as? ThirdViewController
{
vc.doSameMethod()
}
基本上,我使用if let
語句檢查可選的nil,然后將其展開並分配。
我在所有3個ViewController中都具有doSameMethod()
函數,並使用相同的函數名稱執行幾乎相同的任務。
我的問題是,我該怎么做才能避免重復的代碼:
if let vc = (currentVC as? FirstViewController || current as? SecondViewController || currentVC as? ThirdViewController)
{
vc.doSameMethod()
}
顯然,上面的代碼無法編譯,但是我想知道是否存在一種更簡單,更干凈的方法來通過對多種類型進行檢查來解開可選內容?
謝謝!
您可以使用協議來完成。 使用所有3個視圖控制器之間的通用方法定義協議,並使它們符合該協議:
protocol MyViewControllerProtocol {
func doSameMethod()
}
class FirstViewController: UIController, MyViewControllerProtocol {
// your code
}
class SecondViewController: UIController, MyViewControllerProtocol {
// your code
}
class ThirdViewController: UIController, MyViewControllerProtocol {
// your code
}
用法:
if let vc = currentVC as? MyViewControllerProtocol {
vc.doSameMethod()
}
如果您有多個都實現相同方法的視圖控制器,那么按照@CodeDifferent的答案中所述,協議是一個不錯的選擇。
如果每個視圖控制器都有不同的接口,並且您需要根據傳遞給您的視圖控制器執行不同的操作,則可以使用switch語句的變體,並使用case let newConstant as <type>:
這是prepareForSegue方法中使用的示例代碼片段,您可能在其中處理對幾種不同類的視圖控制器之一的segue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
switch segue.destinationViewController {
case let fooViewController as FooViewController:
fooViewController.delegate = self
case let barViewController as BarViewController:
barViewController.data = data
default:
break
}
}
在您的情況下,它將顯示為
switch currentVC {
case let firstVC as FirstViewController:
//code for FirstViewController
case let secondVC as SecondViewController:
//code for SecondViewController
case let thirdVC as ThirdViewController:
//code for ThirdViewController
}
如果currentVC
是指定類的對象,則選擇一種情況。 此外,在case語句內部有一個定義了正確類型的局部常量,就像if let一樣。
編輯-完整示例,摘自現已失效的StackOverflow文檔中的“基於類的匹配-非常適合prepareForSegue”:
您還可以根據要打開的事物的類來使switch語句切換。
在prepareForSegue
,這是一個有用的示例。 我曾經根據segue標識符進行切換,但這很脆弱。 如果您稍后更改情節提要並重命名segue標識符,則會破壞您的代碼。 或者,如果對同一視圖控制器類(但情節提要場景不同)的多個實例使用segues,則無法使用segue標識符來確定目標的類。
快速切換語句以進行救援。
使用Swift的case let var as Class
語法,如下所示:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
switch segue.destinationViewController {
case let fooViewController as FooViewController:
fooViewController.delegate = self
case let barViewController as BarViewController:
barViewController.data = data
default:
break
}
}
在Swift 3中,語法略有變化:
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
switch segue.destinationViewController {
case let fooViewController as FooViewController:
fooViewController.delegate = self
case let barViewController as BarViewController:
barViewController.data = data
default:
break
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.