gpt4 book ai didi

swift - 为什么它说 "Trailing closure passed to parameter of type ' Int' 不接受闭包”?

转载 作者:行者123 更新时间:2023-12-05 04:47:26 27 4
gpt4 key购买 nike

我在 SwiftUI 项目中为 Onboarding Screen 添加了第二个 TabView,它抛出了类似这样的错误

Trailing closure passed to a parameter of type 'Int' that does notaccept a closure

有什么想法吗?

TabView {
ForEach(0 ..< onboardingData.count) { index in
let element = onboardingData[index]
OnboardingCard(onboardingItem: element)
}
}

入职卡:

fileprivate struct OnboardingCard: View {
let onboardingItem: OnboardingItem
var body: some View {
VStack {
Image(onboardingItem.imageName)
.resizable()
.frame(height: 320)
.frame (maxWidth: .infinity)
Text(onboardingItem.title)
.font(.title)
.foregroundColor(.black)
.bold()
.padding()
Text(onboardingItem.description)
.multilineTextAlignment(.center)
.font(.body)
.foregroundColor(.gray)
.padding (.horizontal, 15)
}
}
}

struct OnboardingItem {
let imageName: String
let title: String
let description: String
}

最佳答案

我只是运行你的代码。它按预期工作。


确认 Identifiable 协议(protocol)。

struct OnboardingItem: Identifiable {
var id: UUID = UUID()
let imageName: String
let title: String
let description: String
}

这样做可以避免使用和基于索引的循环。而是这样做:

ForEach(onboardingData) { onboardingItem in
OnboardingCard(onboardingItem: onboardingItem)
}

关于swift - 为什么它说 "Trailing closure passed to parameter of type ' Int' 不接受闭包”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68481300/

27 4 0