在"作用域"中找不到类型 'Content'



我在一个XCode项目上工作,不知道如何摆脱这个错误。我已经导入了SwiftUI库,其中"内容"来自,但XCode似乎没有意识到这一点。有什么建议吗?

func body(content: Content) -> some View {
content
.blur(radius: spotify.isAuthorized ? 0 : 3)
.overlay(
ZStack {
if !spotify.isAuthorized || Self.debugAlwaysShowing {
Color.black.opacity(0.25)
.edgesIgnoringSafeArea(.all)
if self.finishedViewLoadDelay || Self.debugAlwaysShowing {
loginView
}
}
}
)
.onAppear {
// After the app first launches, add a short delay before
// showing this view so that the animation can be seen.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
withAnimation(LoginViewController.animation) {
self.finishedViewLoadDelay = true
}
}
}
}

您正在使用Content而没有告诉编译器有关它的足够详细信息。在ViewModifierAnimatableModifier中,Content已经被SwiftUI库定义为associatedType

如果你在View或自定义结构类型中,Content不会被定义,但你可以让编译器知道它是View,这样做:

struct MyStruct<Content> where Content : View {
func body(content: Content) -> some View {
return content.background(Color.black)
}
}

如果它是一个ViewModifier,就像我描述的,编译器已经知道约束,你不会看到任何错误:

struct ExampleStruct : ViewModifier {
func body(content: Content) -> some View {
return content.background(Color.black)
}
}
struct ExampleStruct2 : AnimatableModifier {
func body(content: Content) -> some View {
return content.background(Color.black)
}
}

相关内容

最新更新