运行时错误:前提条件失败:属性未能设置初始值 [英] Runtime error: precondition failure: attribute failed to set an initial value

查看:25
本文介绍了运行时错误:前提条件失败:属性未能设置初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图 BugSplitView 单独工作正常但会导致

I have a view BugSplitView which works fine alone but causes a

前提失败:属性设置初始值失败

precondition failure: attribute failed to set an initial value

在预览或模拟器中导航到时出错.

error when navigated to in either preview or the simulator.

该视图具有由水平按钮栏分隔的上部 (Color) 和下部 (Color),并使用 GeometeryReader 和 split 状态进行布局.当它是 NavigationButton 的目的地时,它不会在预览中正确显示并在模拟器中运行时报告上面的断言.删除 BugButtonBar 并且它可以工作.把我难住了!帮助.

The view has an upper part (Color) and a lower part (Color) separated by a horizontal button bar and laid out using the GeometeryReader and a split state. When it is the destination of NavigationButton it doesn't show properly in the Preview and reports the assertion above when run in the simulator. Remove the BugButtonBar and it works. Got me stumped! Help.

import SwiftUI

struct BugSplitView: View {
    @State var split : CGFloat = 0.75
    var buttons : [BugButtonBar.Info]{
        [BugButtonBar.Info(title: "title", imageName: "text.insert"){}]
    }
    var body: some View {
        GeometryReader{ g in
            VStack(spacing: 0){
                Color.gray
                    .frame(width: g.size.width, height: (g.size.height) * self.split)
                VStack{
                    BugButtonBar(infos: self.buttons)
                    Color(white: 0.3)
                }
                    .frame(height: (g.size.height) * (1 - self.split))
            }
        }.edgesIgnoringSafeArea(.all)
    }
}


struct BugButtonBar : View{

    struct Info : Identifiable {
        var id = UUID()
        var title : String
        var imageName : String
        var action: () -> Void
    }

    var infos : [Info]
    func color() -> Color{
        Color.black
    }
    var body: some View {
        HStack(){
            Spacer()
            ForEach(self.infos){ info in
                Button(action: info.action){
                    Text(info.title)
                }
                Spacer()
            }
        }
    }
}


struct ShowBugView : View{
    var body : some View{
        NavigationView {
            NavigationLink(destination: BugSplitView()){
                Text("Show Bug")
            }
        }
    }
}


struct BugSplitView_Previews: PreviewProvider {
    static var previews: some View {
        Group{
            BugSplitView()
            ShowBugView()
        }
    }
}

推荐答案

问题是您的按钮被声明为计算属性.要解决崩溃,请像这样声明它们:

The problem is that your buttons are declared as computed property. To solve the crash declare them like this:

var buttons = [BugButtonBar.Info(title: "title", imageName: "text.insert"){}]

这篇关于运行时错误:前提条件失败:属性未能设置初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆