1

在 SwiftUI 中,我想展示一个List但是我希望我的每个屏幕/页面对象拥有它自己的标题和图像,以便在NavigationLink.

所以我有一个我的每个屏幕都可以使用的协议:

protocol DataScreen: View {
    var title: String { get async }
    var systemImage: String { get }
}

我创建了一些屏幕:

struct SystemInfoScreen: DataScreen {
    
    var title: String {
        get async {
            await Translator.shared.translate("sysInfo.title") ?? "System Information"
        }
    }
    
    var systemImage: String = "message"
    
    var body: some View {
        Text("SystemInfoScreen")
    }
}

如您所见,我正在使用一个async函数来查找文件中的文本以返回正确的标题。

func translate(_ string:String?) async -> String? { ... }

我遇到的问题是asyncSwiftUI. 我还很陌生,SwiftUI但这是我到目前为止所拥有的:

struct SidebarView: View {
    var body: some View {
        List {
            CustomNavigationLink.navigationLink(screen: SystemInfoScreen())
        }
    }
}

struct CustomNavigationLink {
    
    @ViewBuilder
    static func navigationLink<S>(screen:S) -> some View where S: DataScreen {
        NavigationLink(
            destination: screen,
            label: {
                Label(screen.title, systemImage: screen.systemImage )
            })
    }
}

这给出了一个错误screen.title

不支持并发的函数中的“异步”属性访问

我有点没想到它会按原样工作,但是有可能以async var这种方式使用它吗?

4

0 回答 0