0

I've a very similar issue to this question, but I don't know how to fix it.

I have a swift bridging header file to expose ObjC classes to swift and a swift interface header (automatically generated by Xcode during compile time) to expose swift classes to ObjC.

I am using swift types in an ObjC file, say A.mm, so I've included the swift interface header in that file.

Recently, I extended my AppDelegate to conform to UNUserNotificationCenterDelegate to handle notifications.

import      UIKit
import      UserNotifications

extension AppDelegate: UNUserNotificationCenterDelegate {

    ....
}

But this suddenly causes a compilation error in A.mm - 'Cannot find protocol declaration for 'UNUserNotificationCenterDelegate'. In the swift interface header file, I can see that UNUserNotificationCenterDelegate is referenced, so just adding this line before importing the swift interface header will work.

#import <UserNotifications/UserNotifications.h>

But it makes no sense to do so - since the code in A.mm has nothing to do with UserNotifications. I had to include swift interface header file here because I need to use some swift types, but none of that is related to UserNotifications. So, I'm not comfortable in importing UserNotifications.h as it just causes confusion. I think the swift interface header should somehow include UserNotifications.h, since that's where the problem originates.

Following this answer, I ensured to include

import      UserNotifications

in the AppDelegate file, as shown above... but it still fails to compile. So adding the swift include is not the solution.

What am I missing to ensure that the swift interface header file includes the UserNotifications.h file? Is there no way but to include UserNotifications.h before including the swift interface header in A.mm? (I don't like this).

NightFuryLxD
  • 847
  • 5
  • 15
  • It makes perfect sense that you need to `#import ` - you are exposing your AppDelegate from Swift to Objective C. That AppDelegate conforms to the notification delegate, so the objective C side of your code needs to know about that. It doesn't matter that A.mm has nothing to do with notifications. The Objective C compiler needs to be able to resolve all of the type references it sees in the header. – Paulw11 Apr 18 '23 at 10:57
  • @Paulw11 In the swift interface header file, there is this line `@import UserNotifications` along with other @imports and @class definitions. But it was wrapped in a macro, so maybe it got excluded. What to do to have `@import UserNotifications` in the swift interface header file? Maybe something's missed in the swift file which has defined UNNotificationCenterDelegate. Since, swift interface header has included this reference, can it not include missing references? – NightFuryLxD Apr 19 '23 at 11:27

0 Answers0