ios - Are there benefits to checking for segue identifiers rather than using failed type casting? -
when implement prepareforsegue:sender:
method in uiviewcontroller
subclass has multiple segues, 1 idiom in objective-c assign identifier segue in storyboard, , wrap logic in prepareforsegue:sender:
in if
statement inspects segue identifier
. example:
if segue.identifier == "destinationa" // prepare destinationa stuff else if segue.identifier == "destinationb" // prepare destinationb stuff ...
in swift, we're forced api use type casting when obtaining instance of destination view controller. since can fail gracefully, my question is should go ahead , rely on optional binding conditional type casting (as?
) or should still rely on if
statement?
for example, possible, should favor succinctness of relying on type casting:
override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if let destination = segue.destinationviewcontroller as? destinationacontroller { // prepare stuff destination } else if let destination = segue.destinationviewcontroller as? destinationbcontroller { // prepare stuff destination b } }
or there benefit in still wrapping in if
statement did in objective-c:
override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if segue.identifier == "destinationa" { if let destination = segue.destinationviewcontroller as? destinationacontroller { // prepare stuff destination } } else if segue.identifier == "destinationb" { if let destination = segue.destinationviewcontroller as? destinationbcontroller { // prepare stuff destination b } } }
note: realize switch can used, that's not point here.
relying on identifier has benefit if source/destination view controller got changed class, can catch more because fall right identifier bucket fail downcast. strictly speaking, code path not same. more succinct style, failed case won't caught until after downcasts have been performed.
override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if segue.identifier == "destinationa" { if let destination = segue.destinationviewcontroller as? destinationacontroller { // prepare stuff destination } else { print("something wrong controller destination a!") } } else if segue.identifier == "destinationb" { if let destination = segue.destinationviewcontroller as? destinationbcontroller { // prepare stuff destination b } } }
vs.
override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if let destination = segue.destinationviewcontroller as? destinationacontroller { // prepare stuff destination } else if let destination = segue.destinationviewcontroller as? destinationbcontroller { // prepare stuff destination b } else { print("something wrong controller!") } }
another benefit can other logic after identifier test , before performing downcast.
it's quicker read code using identifier in terms of intent - not shorter, identifier being swift string can made quite descriptive.
Comments
Post a Comment