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

时间:2021-01-21 20:43:24

标签: swiftui swiftui-navigationlink

我有一个视图 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)
    }

如您所见,当点击“价格”按钮时,应用显示 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
    }
}

我很担心。帮我!谢谢。

0 个答案:

没有答案