'How to exclude code from AppExtension? @available(iOSApplicationExtension, unavailable) does not work

I use a custom tool class across different projects which provides some basic, shared methods.

While I can use this class in most projects without any problem, I cannot compile one project any more, which includes an AppExtension / Widget.

This code fails:

class var applicationInterfaceStyle: UIUserInterfaceStyle {
    // deactivated method code and only return default value...        
    return .unspecified
}

'UIUserInterfaceStyle' is only available in application extensions for iOS 12.0 or newer

I tried to add @available(iOSApplicationExtension, unavailable) to this message, but the error is still the same:

@available(iOSApplicationExtension, unavailable)
class var applicationInterfaceStyle: UIUserInterfaceStyle {
    // deactivated method code and only return default value...        
    return .unspecified
}

Why doesn't this exclude the method completely from being compiled for the extension?

Xcode itself proposes to fix the error by adding @available(iOSApplicationExtension 12.0, *). But this does not work as. Additionally I would prefer to exclude the code completely from the extension. How can this be done?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source