简体   繁体   English

如何创建一个将弱自身传递到闭包内部的闭包签名

[英]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 我创建了一个名为VerifyObject的类,该类包含具有以下签名的函数

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. 当我运行这些runOn...闭包时,会runOn...需要在闭包内使用self引用,捕获self并产生泄漏的情况。

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. 我已经阅读了有关迅速创建weak self ,但是文字太过夸张,以至于我无法回头。

I know the objective-c way but that is considered heresy on swift. 我知道使用objective-c的方法,但是很快就将其视为异端。

How do I create these typealias or the function signature or whatever to pass a weak self to inside each of these 4 closures. 如何创建这些类型typealias或函数签名,或将弱自身传递到这4个闭包中的任何东西。

Please explain like I am 5 years old. 请解释一下我今年5岁。 Ok, make it 4. 好,做4。

Thanks. 谢谢。

When you call handleCodeID you can pass weak self so you can remove any strong reference cycles. 调用handleCodeID您可以传递weak self以便删除任何强引用循环。

So when you call verifyObject(...) it will be something like... 因此,当您调用verifyObject(...) ,它将类似于...

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 typealias实际上并不包含此内容,它是您在调用指定的typealias或任何闭包时使用的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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