首页 > 解决方案 > SwiftUI - 正确使用 @available() 和 #available

问题描述

我在以下代码中遗漏了什么吗?

此应用程序构建(macOS Monterey b5 / Xcode 13 b5)并在 iOS 15 设备上完美执行——但在 iOS 14.7 设备上导致 SIGABRT...

import SwiftUI

struct ContentView: View {
    @State private var text = "This app causes a runtime error on ios 14.7"
    @available(iOS 15.0, *)
    @FocusState var isInputActive: Bool

    var body: some View {

        if #available(iOS 15.0, *) {
            TextEditor(text: $text)
                .focused($isInputActive)
        } else {    //  ios14 or <
            TextEditor(text: $text)
        }

    }
}

标签: iosxcodeswiftui

解决方案


推荐阅读