When enabling direct C++ interoperability in Xcode, this simple code below
NotificationCenter.default.addObserver(self,
selector: #selector(subjectAreaDidChange(_:)),
name: .AVCaptureDeviceSubjectAreaDidChange,
object: nil)
gives me the following error
Type 'NSNotification.Name?' has no member 'AVCaptureDeviceSubjectAreaDidChange'
But if I switch C++ and Objective-C interoperability to just C / Objective-C then the code compiles without errors.
The issue seems to affect NSNotification.Name constants only from AVFoundation framework (although there maybe more).
Why do I get this error and how do I fix it?
Short answer
It’s a known bug (https://forums.developer.apple.com/forums/thread/733693) for Xcode 15.
You can work it around by using raw strings of needed notifications:
Long answer
If you look inside
AVFoundation/AVCaptureDevice.hyou can find:The interoperability just looks for “Notification” suffix in exported strings and converts them to
NSNotification.Namee.g.:The same works for strings in your app:
Then you can write in your swift file:
But it works for
C / Objective-Conly and doesn't forC++ / Objective-C++because the compiler doesn't generate appropriate properties inNSNotification.Nameextension, for instance: