SHARE
TWEET

Untitled

a guest Dec 16th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class SomeModel : Printable {
  2.  
  3.     var a:String
  4.  
  5.     init(a:String) {
  6.         self.a = a
  7.     }
  8.  
  9.     var description:String {
  10.         return "(self.a)"
  11.     }
  12. }
  13.  
  14.  
  15. class Editor {
  16.  
  17.     var getter:()-> String
  18.     var setter:(String)->()
  19.  
  20.     init(getter:()-> String, setter:(String)->()) {
  21.         self.getter = getter
  22.         self.setter = setter
  23.     }
  24.  
  25.     convenience init(inout bindTo:String) {
  26.         self.init(
  27.             getter:{ return bindTo },
  28.             setter: { v in bindTo = v })
  29.     }
  30.  
  31.     func read() -> String {
  32.         return self.getter()
  33.     }
  34.  
  35.     func write(value:String) {
  36.         self.setter(value)
  37.     }
  38. }
  39.  
  40.  
  41. func testBindTo() {
  42.     var readModel =  SomeModel(a:"Did not capture by reference");
  43.     var bindForReading = Editor(bindTo: &readModel.a)
  44.     readModel.a = "captured by reference!"
  45.     println(bindForReading.read())
  46.  
  47.     var writeModel =  SomeModel(a:"Did not capture by reference");
  48.     var bindForWriting = Editor(bindTo: &writeModel.a)
  49.     bindForWriting.write("captured by reference")
  50.     println(writeModel)
  51. }
  52.  
  53. testBindTo()
  54.  
  55.  
  56. func testExplicitGetterSetter() {
  57.  
  58.     var readModel =  SomeModel(a:"Did not capture by reference");
  59.     var bindForReading = Editor(
  60.         getter: { readModel.a },
  61.         setter: { v in readModel.a = v })
  62.     readModel.a = "captured by reference!"
  63.     println(bindForReading.read())
  64.  
  65.     var writeModel =  SomeModel(a:"Did not capture by reference");
  66.     var bindForWriting = Editor(
  67.         getter: { writeModel.a },
  68.         setter: { v in writeModel.a = v })
  69.     bindForWriting.write("captured by reference")
  70.     println(writeModel)    
  71. }
  72.  
  73. testExplicitGetterSetter()
  74.    
  75. Did not capture by reference
  76. Did not capture by reference
  77. captured by reference!
  78. captured by reference
  79.    
  80. /// A class providing access to a resource with an inout parameter in an escaping closure.  
  81.      class ProtectedResource<ValueType> {
  82.             private var protectedResourceArray = [ValueType]()
  83.             private var protectedResourceArrayLock = NSRecursiveLock()
  84.             private let opq = OperationQueue()
  85.  
  86.             func performWithResource(block: @escaping (inout [ValueType]) -> ()) {
  87.                 opq.addOperation { [weak self] in
  88.                     guard let strongSelf = self else {
  89.                         return
  90.                     }
  91.                     strongSelf.protectedResourceArrayLock.lock()
  92.                     block(&strongSelf.protectedResourceArray)
  93.                     strongSelf.protectedResourceArrayLock.unlock()
  94.                 }
  95.             }
  96.         }
  97.  
  98. /// Some other class using that in out parameter.
  99.  
  100.         func run() {
  101.  
  102.             func updateArray(array: inout [String]) {
  103.                 print("Performing on (array)")
  104.                 array.append("test")
  105.             }
  106.  
  107.             protectedResource.performWithResource(block: updateArray)
  108.  
  109.             protectedResource.performWithResource {
  110.                 print("Performing on ($0)")
  111.             }
  112.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top