I’m migrating a NotificationManager class to Swift 6. This class is a small utility wrapper round NotificationCenterand has a technique that lets customers register a notification that can triggerunless the object is the same as a selected NSObjectworth.
For instance:
supervisor.registerObserver(for: title, forObject: nil, ignoreIfSentFrom: self) {
// run this code _only_ if the sender wasn't self
}
The strategy seems to be like this:
non-public func registerObserver(_ title: NSNotification.Title, forObject object: AnyObject?,
ignoreIfSentFrom ignoredObject: NSObject, block: @Sendable @MainActor @escaping (Notification) -> ())
{
let newToken = NotificationCenter.default.addObserver(forName: title, object: object, queue: nil) { word in
guard (word.object as AnyObject) !== ignoredObject else { return }
Process { @MainActor in
block(word)
}
}
observerTokens.append(newToken)
}
I get two errors right here that I can’t determine find out how to resolve:
Seize of 'ignoredObject' with non-Sendable sort 'NSObject?' in a '@Sendable' closure(on theguardline)Sending 'word' dangers inflicting knowledge races; that is an error within the Swift 6 language mode(forblock(word))
Is it nonetheless attainable to implement this concept with Swift 6 strict concurrency? It seems to be like Notification is neither Sendablenor @MainActor and since I don’t personal that sort, I’m at a loss for find out how to make this work.
