14

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?

Greg
  • 10,360
  • 6
  • 44
  • 67
  • Objective C doesn't distinguish been escaping and non-escaping blocks, so it wouldn't surprise me if all ObjC interfaces imported into Swift as escaping. – Alexander Oct 18 '18 at 17:32
  • It would be manual but does NS_SWIFT_NAME allow you to pass that info across – Warren Burton Oct 18 '18 at 17:32

1 Answers1

27

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 ^).

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • This probably goes back to before the time when we had `@escaping` and we had `@noescape` instead. So that got baked into Objective-C and now there's no point changing it even though Swift has switched polarities, as it were. – matt Oct 18 '18 at 17:50
  • 2
    Just a remark (because that bit me): You'll see the @escaping in the Swift generated interface only if the Objective-C block parameter is marked as _Nonnull. Otherwise it is imported as IUO, and those are escaping by default. – Martin R Oct 18 '18 at 18:46
  • @MartinR Yes, I see that a `(^ __nullable)` block lacks the `@escaping` parameter even though it does in fact escape. Hmmm. – matt Oct 18 '18 at 19:04
  • 2
    Optional closures are “implicitly escaping:” https://bugs.swift.org/browse/SR-2324, https://stackoverflow.com/a/39846519/1187415, https://oleb.net/blog/2016/10/optional-non-escaping-closures/. – Martin R Oct 18 '18 at 19:09
  • @MartinR So weird. - I've added "nonnullable" into my statement of the rule. – matt Oct 18 '18 at 19:11
  • @matt More likely, I think the default behaviors in Objective-C have remained backward-compatible with older Obj-C/C code code, with annotations for modern code safety assertions. This has made writing ObjC in modern times more messy with annotations _(when was the last time you saw an Obj-C header that didn’t have `NS_ASSUME_NONNULL_BEGIN`/`END` and `nullable` littered throughout?),_ but non-annotated Obj-C works (unsafely) the same as it always has, and annotated Obj-C bridges nicely into Swift. – Slipp D. Thompson Jan 01 '23 at 15:00