Home > Enterprise >  Swift error some return types are only available on macOS 10.12
Swift error some return types are only available on macOS 10.12

Time:06-08

I have the following code however I am getting an error

some return types are only available in macOS 10.15.0 or newer

I do not want to add the @available on this. Is there a another way that I can restructure the follow code

public extension Action
{
    static var none: some Action { NoAction() }
    
    // -------------------------------------
    /** other code below
}

I have a struct in a different file noAction.swift

public struct NoAction: Action
{
    public init() { }
    
    // -------------------------------------
    public var keyEquivalent: KeyEquivalent?
    {
        get { nil }
        set { }
    }
    
//other code same structure
}

CodePudding user response:

As it notes, some return types are only available in macOS 10.15.0 or newer. So if you want to run on older versions, you cannot return a "some" type (i.e. an opaque type). You'll need to return a non-opaque type:

static var none: NoAction { NoAction() }

Alternately, you could return an existential type (i.e. a box around the protocol). That introduces some limitations on how it's used, but may match your needs:

static var none: Action { NoAction() }
  • Related