0

After updating to Xcode 14, iOS 16, Swift 5.7, when compiling the project I got:

Sendability of function types in instance method 'addObserver(forName:object:queue:using:)' does not match requirement in protocol 'NotificationCenterProtocol'
Nikolay Suvandzhiev
  • 8,465
  • 6
  • 41
  • 47

1 Answers1

4

tl;dr:

If you have a custom protocol and you're conforming an existing class to it, make sure the method signature matches exactly, in this case add @Sendable to the closure (after @escaping).



This can happen when you create a protocol and then conform an existing class to it (in this case the native NotificationCenter). You have to get the signatures of the methods in your protocol to exactly match the existing signatures in the class.

With the latest update, @Sendable was introduced and needs to be added to your custom protocol, so for example for NotificationCenter this function needs to say: [...] using block: @escaping @Sendable (Notification) -> Void.

Tip: The easiest way to see the exact function signature is via the 'Developer Documentation' (under 'Help' menu), because there you can see the Swift syntax. If you were to 'Cmd + Click' on NotificationCenter, you'd see the methods but with the Objective-C syntax.

Nikolay Suvandzhiev
  • 8,465
  • 6
  • 41
  • 47
  • What do you think about this warning then https://stackoverflow.com/questions/76251095/sendability-of-function-types-in-property-does-not-match-requirement-in-protocol – Siddhant Mishra May 15 '23 at 07:55
  • I replied there. An odd mismatch between apparent method signature and what the complier sees. Not sure if it's a bug or something else going on. – Nikolay Suvandzhiev May 16 '23 at 14:27