简体   繁体   中英

How to create a closure signature that passes weak self to inside the closure

I have created a class called VerifyObject , that contains a function with the following signature

typealias handlerCodeID = (String) ->Void
typealias handlerCode = (Date, Code) ->Void
typealias handlerError = (NSError) ->Void


class func verifyObject(withID:String?,
  runOnEnter: handlerCode?,
  runOnExit: handlerCode?,
  runOnPause: handlerCodeID?,
  runOnError: handlerError?) 

when I run these runOn... closures, I will have situations where I need to use references to self inside the closures, capturing self and creating leaks.

I have read everything I could find about creating weak self on swift but the text is so exoteric that I cannot wrap my head around.

I know the objective-c way but that is considered heresy on swift.

How do I create these typealias or the function signature or whatever to pass a weak self to inside each of these 4 closures.

Please explain like I am 5 years old. Ok, make it 4.

Thanks.

When you call handleCodeID you can pass weak self so you can remove any strong reference cycles.

So when you call verifyObject(...) it will be something like...

Self.verifyObject(“someString”, { [weak self] date, code in 
//runonenter
},
{ [weak self] date, code in
 //runonexit
 }, 
{ [weak self] string in
 //runonpause
 },
{ [weak self] error in
 //runonerrir
})

You can continue this

The typealias doesn't actually contain this, it's something you use when calling the specified typealias or any closure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM