SwiftUI Widget: 'Expected a type' error on UILabel argument in an imported Objective-C header file

391 Views Asked by At

I'm encountering a build error when importing an Objective-C class (via bridging header) into my SwiftUI widget extension target.

The Objective-C class defines a method method which accepts a UILabel argument. When building the widget, I receive an error on the method declaration on ‘UILabel’: ‘Expected a type’. I don't actually need to leverage this method in my SwiftUI project but it's part of the class that I do need to import.

I tried adding the UIKit framework to the Widget Extension target, and tried importing the UIKit header within the swift bridging file. No luck, I receive the same error.

I'm wondering if this is an incompatibility issue with using SwiftUI on the target and UIKit in the imported Objective-C file? Is there anything else I need to do to allow my SwiftUI extension to recognize UIKit classes?

2

There are 2 best solutions below

0
On

Yup. So you need to set the "Objective-C BridgingHeader" from the SwiftUI Widget target, and the Objective-C files from the header gets imported to the SwiftUI class.

0
On

The problem was not my widget extension or sharing UIKit classes with SwiftUI. I realized that my bridging header was shared across other extensions (Siri, Watch) which did not support UIKit (or at least do not natively include UIKit frameworks). Adding UIKit dependent headers into the shared bridging header caused those other extensions to fail building.

To solve my problem, I created a second bridging header associated to the widget target only where I'm now able to include UIKit dependent Objective-C classes.