iOS LiveActivity动态岛编译错误

问题描述 投票:0回答:1

我是结构动态岛详细内容

dynamicIsland: { context in
            DynamicIsland {
                expandedContent(context: context)
            } compactLeading: {
                ....
            } compactTrailing: {
                ...
            } 

我想根据上下文显示不同的内容。

private func expandedContent(context: ActivityViewContext<xxxx>)->DynamicIslandExpandedContent<some View> {
        if (context.state.style == 0) {
            return expandedControlContent1(context: context)
        } else if (context.state.style == 1) {
            return expandedControlContent2(context: context)
        } else {
            return expandedControlContent3(context: context)
        }
    }

编译错误

Function declares an opaque return type 'some View', but the return statements in its body do not have matching underlying types
ios swift swiftui activitykit dynamic-island
1个回答
0
投票

您需要用

@DynamicIslandExpandedContentBuilder
标记您的函数,因为 if 语句返回的内容不是同一类型:

@DynamicIslandExpandedContentBuilder
private func expandedContent(context: ActivityViewContext<xxxx>)->DynamicIslandExpandedContent<some View> {
    if (context.state.style == 0) {
        return expandedControlContent1(context: context)
    } else if (context.state.style == 1) {
        return expandedControlContent2(context: context)
    } else {
        return expandedControlContent3(context: context)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.