Home > Software engineering >  Xcode swift ui error 'KeyPathComparator' is only available in macOS 12.0 or newer
Xcode swift ui error 'KeyPathComparator' is only available in macOS 12.0 or newer

Time:03-29

enter image description here

I am getting this error, as you can see from the image I already do what it tells me how to correct the error.

What am I doing wrong?

CodePudding user response:

Unfortunately what you are looking to do is not possible.

The compiler error is generated because the compiler cannot apply @available attributes to properties which have a property wrapper applied to them. However, even if you remove the @State property wrapper and instead wrap the property in a State manually, like this

@available(macOS 12, *) private var sortOrder = State(wrappedValue: [KeyPathComparator(\Repository.name)])

you get a different compiler error:

Stored properties cannot be marked potentially unavailable with '@available'

So as the new compiler error tells you, you cannot mark stored properties as @available without marking the enclosing type @available.

To resolve the issue, you need to create 2 versions of your View, 1 that's marked @available(macOS 12, *) and contains the KeyPathComparator and another one without the @available that doesn't contain the KeyPathComparator property.

  • Related