Home > Mobile >  Why can't I do this using swift conditionally compiling?
Why can't I do this using swift conditionally compiling?

Time:11-04

Compiler will show mistakes when I do this:

#if os(iOS)
class AView: UIView {
#else
class AView: NSView {
#endif
    
}

Why can't I do this?

Does it have any methods to do this thing?

CodePudding user response:

Use conditional compiling with a typealias instead:

#if os(iOS)
typealias ParentView = UIView
#else
typealias ParentView = NSView
#endif

class AView: ParentView {}

Then you'll be able to extend that type alias.

Playground

  • Related