[英]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.