[swift-users] about capture list in nested function

Седых Александр se-a at mail.ru
Wed Jun 14 10:08:30 CDT 2017


Hello

For me more good use nested function instead of closure. How in this case right way to define capture list?


//With Closure
 
class Sender {
    
     var color =  "Orange"
    
     var writer =  Receiver ()
    
     func something() {
         writer . callback = { [weak  self ] newColor  in
             guard let strongSelf =  self else {  return }
            strongSelf. color = newColor
        }
    }
}
 
class Receiver {
     var callback: (( String ) ->  Void )?
    
     func somework() {
         var color =  "green"
         guard let cb =  callback else {  return }
        cb(color)
    }
}
 
//Alternatively with Nested Function
 
class ASender {
    
     var color =  "Orange"
    
     var writer =  Receiver ()
    
     func something() {
         writer . callback =  change
    }
    
     func change(name:  String ) {
         weak var n =  self // is right or not?
         guard let strongSelf = n  else {  return }
        strongSelf. color = name
    }
}
 

-- 
Alex Sedykh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170614/6b8c6df5/attachment.html>


More information about the swift-users mailing list