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).

enter image description here

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

Popular posts from this blog

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

arrays - Algorithm to find ideal starting spot in a circle -

php - Autoloader issue not returning Class -