无法使用 SwiftUI 推断复杂的闭包返回类型 [英] Unable to infer complex closure return type with SwiftUI
问题描述
遵循 Apple SwiftUI 教程的第 3 部分,
struct LandmarkRow:查看{var地标:地标var主体:一些视图{堆栈{地标.图像(大小:50)文本(逐字逐字:地标.名称)垫片()如果地标.isFavorite {图像(系统名称:star.fill").imageScale(.medium)}}}}
不管问题的原因是什么,你怎么能在这里添加一个明确的类型来消除歧义?在这种情况下,返回类型是什么?
--
更新
显然你不应该用第三个教程继续第二个教程的结果.在本教程中没有记录的教程之间发生了一些变化.我已将项目文件添加到 Github,因此您可以7消除差异.
最好重新下载 第三个教程.
问题不在于闭包,而在于地标上的 isFavorite
属性.
它没有在Landmark
类型上声明,编译器反而显示未声明的属性错误,无法解析堆栈构建闭包返回类型,所以它在那里显示和错误.
Apple 教程编写者干得好,一个 Xcode11 编译器甚至更好.
修复:
- 在
Landmark
类型上声明isFavorite
变量. - 确保使用
isFavorite = false
条目更新每个地标记录的landmarkData.json
,否则应用程序将在运行时崩溃.
Following part 3 of Apple's tutorial on SwiftUI, Handling User Input, I get this error:
Unable to infer complex closure return type; add explicit type to disambiguate
I'm using the same code as the tutorial (even coping from the 'Complete' sample code doesn't resolve the error).
Does anyone have a cue what is going wrong here?
struct LandmarkRow: View {
var landmark: Landmark
var body: some View {
HStack {
landmark.image(forSize: 50)
Text(verbatim: landmark.name)
Spacer()
if landmark.isFavorite {
Image(systemName: "star.fill")
.imageScale(.medium)
}
}
}
}
Regardless of the cause of the issue, how could you indeed add an explicit type to disambiguate here? What would the return type in such a case be?
--
Update
Apparently you should not continue with your result of the 2nd tutorial with the 3rd. Some things changed in between of the tutorials that is not documented in the tutorial. I've added the project files to Github so you can check out the diff.
It's better to start the 3rd tutorial fresh with a fresh download of the Project files of the 3rd tutorial.
The issue is not with the closure, but with the isFavorite
property on landmark.
It is not declared on the Landmark
type, and the compiler instead of showing the undeclared property error, unable to resolve the stacks build closure return type, so it shows and error there.
Great job Apple tutorial writers and even better one Xcode11 compiler.
To fix:
- Declare
isFavorite
variable on theLandmark
type. - Make sure you update the
landmarkData.json
for every landmark record with theisFavorite = false
entry, otherwise the app will crash at runtime.
这篇关于无法使用 SwiftUI 推断复杂的闭包返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!