ios - Can't I downcast inside a switch ? (Swift 3) -
i have 2 segues in same prepareforsegue, 1 connects uiviewcontroller subclass , other connects uicollectionviewcontroller subclass. i'm trying use switch/case polymorphism set destinationviewcontroller downcast inside switch not visible outside it, keeps seeing var uivc declared originally. doing wrong here ? error "value of type 'uiviewcontroller' has no member 'context'"
let identifier: string! var destinationtest: uiviewcontroller! switch identifier { case "add exercise": destinationtest = segue.destination as! addexerciseviewcontroller case "addexerciseincollectionview": destinationtest = segue.destination as! addexercisecollectionvc default: break } destinationtest.context = self.context
as rmaddy said, have declared destinationtest
uiviewcontroller
, though assigning specific uiviewcontroller
subclasses inside switch, compiler knows destinationtest
uiviewcontroller
, can't access subclass properties via variable.
you either need 2 view controller subclasses inherit common superclass, defines context
property , declare destinationtest
of superclass, or use protocol provides context
property:
property hascontext { var context: context { set } } class addexerciseviewcontroller: uiviewcontroller, hascontext { var context: context ... } class addexercisecollectionvc: uiviewcontroller, hascontext { var context: context ... }
then can say
let identifier: string! var destinationtest: hascontext! switch identifier { case "add exercise": destinationtest = segue.destination as! hascontext case "addexerciseincollectionview": destinationtest = segue.destination as! hascontext default: break } destinationtest.context = self.context
Comments
Post a Comment