Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Objective-C have an equivalent to Swift's @escaping annotation?

In Swift, escaping closure parameters are annotated with @escaping. Is there any equivalent in Objective-C so that the generated Swift interfaces will be marked as @escaping?

like image 459
Greg Avatar asked Oct 18 '18 17:10

Greg


People also ask

What is Ns_noescape?

So what does an NS_NOESCAPE mean? (It's actually imported as @noescape .) It means that any argument that is passed inside this closure, which is void for void is not going to escape the runtime of the function itself.

What is @escaping Swift?

In Swift, a closure marked with @escaping means the closure can outlive the scope of the block of code it is passed into. In a sense, @escaping tells the closure to “stay up even after the surrounding function is gone”.

Why do you need to declare closures escaping?

In Swift, closures are non-escaping by default. This means that the closure can't outlive the function it was passed into as a parameter. If you need to hold onto that closure after the function it was passed into returns, you'll need to mark the closure with the keyword @escaping.


1 Answers

Yes, but it's backwards from what you suggest in your question. The rule is that an Objective-C nonnullable block is translated into Swift as an @escaping function automatically, unless it is explicitly marked (NS_NOESCAPE ^).

like image 175
matt Avatar answered Sep 17 '22 07:09

matt