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
  • 9,783
  • 5
  • 41
  • 64
  • 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

25

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
  • 485,702
  • 82
  • 818
  • 1,064
  • 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