我是结构动态岛详细内容
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
您需要用
@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)
}
}