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'
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.