我正在尝试动态确定 AppIntent 是否按照
openAppWhenRun
属性的指示打开应用程序。有时应该打开应用程序,有时则不应该。
我尝试过的
Function declares an opaque return type 'some IntentResult & OpensIntent', but the return statements in its body do not have matching underlying types
func perform() async throws -> some IntentResult & OpensIntent {
if (condition) {
return .result(opensIntent: OtherIntent())
}
return .result(opensIntent: NestedIntent())
}
openAppWhenRun
不是静态的并在运行时更改它,但这不起作用需要注意的是,我也不想使用
needsToContinueInForegroundError()
或 requestToContinueInForeground()
,因为它们需要用户确认。
您收到的错误是由于 尝试返回不同类型作为
some Type
。
这与 SwiftUI 中 @ViewBuilder 通过将结果包装为单一类型来处理的事情相同。但是,由于这涉及静态属性,因此包装类型(我相信)不能在这里工作。
所以看来你不能根据条件调用不同的意图类型。 此讨论也表明您的要求是不可能的。
现在您当然可以链接意图。但据我所知,打破这个链条的唯一方法是抛出错误。这给了我们一个令人难以置信的 hacky 解决方案:
enum DoneError: Error, LocalizedError {
case done
var errorDescription: String? { "All done!" }
}
func perform() async throws -> some IntentResult {
// do things...
if condition {
throw DoneError.done
} else {
return .result(opensIntent: OpeningAppIntent())
}
}
...所以你可能应该以不同的方式标记意图。