swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.42k stars 10.34k forks source link

[cxx-interop] NSNotification.Name has no member #69914

Closed tcollins590 closed 4 months ago

tcollins590 commented 10 months ago

Description I have a swift package that I am trying to add c++ interop to. The package compiles until I add swiftSettings: [.interoperabilityMode(.Cxx)]. I'm then presented with a few errors in my existing swift code

The code

NotificationCenter.default.addObserver(self,
                                       selector: #selector(self.changeCamera),
                                       name: NSNotification.Name.AVCaptureDeviceWasConnected,
                                       object: nil)

NotificationCenter.default.addObserver(self,
                                          selector: #selector(self.changeCamera),
                                          name: NSNotification.Name.AVCaptureDeviceWasDisconnected,
                                          object: nil)

The error Type 'NSNotification.Name' has no member 'AVCaptureDeviceWasConnected Type 'NSNotification.Name' has no member 'AVCaptureDeviceWasDisconnected

Steps to reproduce Add an observer to the NotificationCenter to observe NSNotification.Name.AVCaptureDeviceWasDisconnected. Compile without swift interop and then attempt to compile with swift interop

Expected behavior I expect this to compile with swift interop just as it does without interop enabled

Environment

AnthonyLatsis commented 10 months ago

cc @egorzhdan

egorzhdan commented 10 months ago

Thanks, this is tracked internally as rdar://115424525, and we're working to fix this in https://github.com/apple/swift/pull/69776

tcollins590 commented 10 months ago

@egorzhdan Thanks for the update. I've found several other pieces of swift code that break when enabling interop such as pieces of ARKit. Would you like those as separate issues?

egorzhdan commented 10 months ago

@tcollins590 yes, please! The issues with NSNotification.Name are all caused by the same underlying problem, so there's no need to report them, but other cases of source breakage caused by C++ interop are definitely worth reporting separately.

knielsen-foreflight commented 10 months ago

Is there a workaround for this issue?

tcollins590 commented 9 months ago

@knielsen-foreflight sorry for the delay in response. As a temporary workaround I implemented a polling based solution for the specific use case. In my case I needed to see if an external camera was attached. To achieve this I poll the camera configuration to see if there is one attached. This is not an optimal fix but allowed me to solve the issue while I wait for a future fix in swift.

code-per-day commented 8 months ago

I have the same issue for our app, do we have an ETA for when this fix will be released?

ElJabato commented 7 months ago

@knielsen-foreflight sorry for the delay in response. As a temporary workaround I implemented a polling based solution for the specific use case. In my case I needed to see if an external camera was attached. To achieve this I poll the camera configuration to see if there is one attached. This is not an optimal fix but allowed me to solve the issue while I wait for a future fix in swift.

There is a workaround, based in "init raw value". Swift code:

NotificationCenter.default.addObserver(forName: Notification.Name.GCControllerDidConnect, object: nil, queue: nil, using: connectMFIController)

Is equivalent to

NotificationCenter.default.addObserver(forName: Notification.Name.init(rawValue: "GCControllerDidConnectNotification"), object: nil, queue: nil, using: connectMFIController)

Find the string name is possible with "print" (swift):

print(Notification.Name.GCControllerDidConnect)

Output this:

NSNotificationName(_rawValue: GCControllerDidConnectNotification)

(Of course you must set interop to "C / Objective C" firstly).

schwa commented 4 months ago

This just hit me and I spent way too long trying to understand what caused it. Is there an ETA? I see the fix is merged but it's unavailable to me as of Xcode 15.4 and the macOS 14 SDK

AnthonyLatsis commented 4 months ago

Is there an ETA?

Apparently this was not nominated for 5.10, so it will ship with 6.0, the next release.