Hi, community:
Today a Swift question came up to me. Is a closure enum associated value considered to be an escaping closure?
an example:
enum Operation {
case opA(()-> Void)
case opB(()-> Void)
}
Thanks in advance
You should consider the closure to be escaping. The syntax for declaring an enum associated value isn’t quite the same as an initializer so there’s nowhere to put the @escaping
keyword, but the effect is the same: the closure is retained by the enum value and may be called later. If you do this in a class context and implicitly reference self
then you’ll get the familiar warning about that:
class Foo: NSObject {
func foo() {
_ = Operation.opA {
print(description) // ERROR: Implicit use of 'self' in closure; use 'self.' to make capture semantics explicit
}
}
}