SwiftUI 崩溃"AttributeGraph precondition failure: attribute failed to set an initial value"



我有一个视图MachineDetailsView

下面是代码

internal struct MachineDetailsView {
@State internal var mediaPageIndex = 0
@State internal var showPriceView = false
internal var mediaViews: [MediaView] {
var views = [MediaView]()

guard let medias = machine?.media else { return views }

medias.forEach {
views.append(MediaView(media: $0))
}

return views
}
internal var body: some View {
ZStack(alignment: .topLeading) {
VStack {
PagerView(
index: $mediaPageIndex,
leftInset: 0.15,
rightInset: 0.15,
pages: mediaViews
)
.frame(height: 238)

PagerDotsView(
index: $mediaPageIndex,
pages: mediaViews
)
}
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color(.brand))
.padding(.vertical, Spacing.extraTiny)

Button(action: {
self.showPriceView = true
}) {
Text("Price")
.modifier(ButtonStyle.Filled())
.padding(.horizontal)
}
}
.navigationLink(destination: LazyView { MachinePriceView(viewModel: self.viewModel) },
isActive: $showPriceView)
}

可以看到,当点击"Price"按钮,应用程序显示MachinePriceView(工作正常)但是当点击MachinePriceView上的导航栏按钮时,应用程序崩溃了,下面的日志。

AttributeGraph precondition failure: attribute failed to set an initial value: 2036248,
ForEachChild<Array<MediaView>, UUID,    ModifiedContent<ModifiedContent<ModifiedContent<ModifiedContent<MediaView, _FrameLayout>, _TransactionModifier>, _OpacityEffect>, _TransactionModifier>>.

我不知道为什么应用程序崩溃。以下是MediaView

的代码
internal struct MediaView: View, Identifiable, Equatable {
internal let id = UUID()
internal let media: Media

internal var body: some View {
VStack {
if media.mediaType == .image {
KFImage(URL(string: media.mediaUrl ?? ""))
.resizable()
.aspectRatio(contentMode: .fit)
.clipped()
} else {
if media.mediaUrl != nil {
YoutubePlayer(videoURL: media.mediaUrl!)
.frame(width: UIScreen.main.bounds.width * 0.8, height: 208)
}
}
}
.frame(width: UIScreen.main.bounds.width * 0.8, height: 208)
.background(Color(.brandContrast))
.cornerRadius(6)
.padding(.horizontal)
}

internal static func == (lhs: Self, rhs: Self) -> Bool {
lhs.id == rhs.id
}
}

我很担心。帮帮我!谢谢你。

海峰的回答对我很管用。我只是删除了随机id (UUID)从可识别的修复崩溃

相关内容

  • 没有找到相关文章

最新更新