繁体   English   中英

编译错误“实例方法 'someMethod' 中 function 类型的可发送性与协议 'SomeProtocol' 中的要求不匹配” iOS 16 Swift5

[英]Compile error "Sendability of function types in instance method 'someMethod' does not match requirement in protocol 'SomeProtocol'" iOS 16 Swift 5.7

更新到 Xcode 14、iOS 16、Swift 5.7 后,在编译项目时我得到:

Sendability of function types in instance method 'addObserver(forName:object:queue:using:)' does not match requirement in protocol 'NotificationCenterProtocol'

tl;博士

如果您有一个自定义协议并且您正在使现有的 class 符合它,请确保方法签名完全匹配,在这种情况下将@Sendable添加到闭包(在@escaping之后)。



当您创建一个协议,然后将现有的 class 符合它(在本例中为本机NotificationCenter )时,可能会发生这种情况。 您必须获取协议中方法的签名才能与 class 中的现有签名完全匹配。

随着最新更新, @Sendable被引入并且需要添加到您的自定义协议中,例如对于NotificationCenter这个 function 需要说: [...] using block: @escaping @Sendable (Notification) -> Void

提示:查看确切的 function 签名的最简单方法是通过“开发人员文档”(在“帮助”菜单下),因为在那里您可以看到 Swift 语法。 如果您要在NotificationCenter上“Cmd + 单击”,您会看到方法,但使用 Objective-C 语法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM