swift3 - What is the Swift equivalency of NS_VALID_UNTIL_END_OF_SCOPE? -
scenario:
i'm translating objective-c sample code supplied apple swift 3.0.1.
came across code requires need prevent presentationcontroller being released prior calling preventviewcontroller. hence use of ns_valid_until_end_of_scope (see image below).
what's best alternative using swift?
...without it... nil transitioningdelegate value upon access afterwards.
one thing can try using withextendedlifetime(_:_:)
:
override func perform() { let sourceviewcontroller = self.destination let destinationviewcontroller = self.destination // presentations use custom presentation controller, // possible presentation controller // transitioningdelegate. // // transitioningdelegate not hold strong reference // destination object. prevent presentationcontroller being // released prior calling -presentviewcontroller:animated:completion: // ns_valid_until_end_of_scope attribute appended declaration. let presentationcontroller = aapladaptivepresentationcontroller(presentedviewcontroller: destinationviewcontroller, presenting: sourceviewcontroller) withextendedlifetime(presentationcontroller) { destinationviewcontroller.transitioningdelegate = presentationcontroller self.source.present(destinationviewcontroller, animated: true, completion: nil) } }
or else, work in case shown in picture:
override func perform() { let sourceviewcontroller = self.destination let destinationviewcontroller = self.destination // presentations use custom presentation controller, // possible presentation controller // transitioningdelegate. // // transitioningdelegate not hold strong reference // destination object. prevent presentationcontroller being // released prior calling -presentviewcontroller:animated:completion: // ns_valid_until_end_of_scope attribute appended declaration. let presentationcontroller = aapladaptivepresentationcontroller(presentedviewcontroller: destinationviewcontroller, presenting: sourceviewcontroller) destinationviewcontroller.transitioningdelegate = presentationcontroller self.source.present(destinationviewcontroller, animated: true, completion: { let _ = presentationcontroller //<- having strong reference in completion handler }) }
Comments
Post a Comment