简体   繁体   中英

How to compare switch case with UIViewController as input in swift 2

I have some code in older version in swift, like bellow

let initVc:UIViewController? = self.initViewController!
        switch initVc {
            case let vc as ProductListViewController:
                pageMenu?.moveToPage(0)
            case let vc as OrderListViewController:
                pageMenu?.moveToPage(1)
            case let vc as CustomerListViewController:
                pageMenu?.moveToPage(2)
            default:
                pageMenu?.moveToPage(0)
        }

in above code self.initViewController is parent view controller from there this controller is pushed.

after updat to swift 2.0 gives and error, like bellow

Immutable value 'vc' was never used; consider replacing with '_' or removing it

Please suggest changes required to fulfil this requirement.

Equivalent code without assigning a value to an (unused) variable would be:

switch initVc {
    case is ProductListViewController:
        pageMenu?.moveToPage(0)
    case is OrderListViewController:
        pageMenu?.moveToPage(1)
    case is CustomerListViewController:
        pageMenu?.moveToPage(2)
    default:
        pageMenu?.moveToPage(0)
}

using the "type-casting pattern" is <Type> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM