Home > Software design >  Unavailable property with a property wrapper
Unavailable property with a property wrapper

Time:07-11

I'm working on an app that supports iOS 14 and up, but I would like to use some of the SwiftUI 3 property wrappers like @FocusState.

How can I work around the Stored properties cannot be marked potentially unavailable error?

Here is the simplified version of our code, it works with Xcode 13, but fails to compile with Xcode 14.

struct MyView: View {
  enum Field: Hashable {
    case username
  }

  @State private var myText: String = ""

  @available(iOS 15, *)
  @FocusState private var focusedField: Field?

  var body: some View {
    if #available(iOS 15, *) {
      TextEditor(text: $myText)
        .focused($focusedField, equals: .username)
    } else {
      TextEditor(text: $myText)
    }
  }
}

I cannot use a computed property with a backing storage workaround as a computed property cannot have a property wrapper.

Are there any other workarounds that I can use except for an almost identical copy of MyView?

CodePudding user response:

A possible approach is to separate all dependent parts into one subview, and mark it as available for new OS.

Here is a demo:

struct MyView: View {
    enum Field: Hashable {
        case username
    }

    @State private var myText: String = ""

    @available(iOS 15, *)            // << here !!
    struct NewTextEditor: View {
        @Binding var text: String
        @FocusState private var focusedField: Field?

        var body: some View {
            TextEditor(text: $text)
                .focused($focusedField, equals: .username)
        }
    }


    var body: some View {
        if #available(iOS 15, *) {
            NewTextEditor(text: $myText)
        } else {
            TextEditor(text: $myText)
        }
    }
}
  • Related