• Xcode编写SwiftUI代码时一个编译通过但导致预览(Preview)崩溃的小陷阱


    在这里插入图片描述

    概述

    在使用Xcode编写SwiftUI代码时,一个小小的疏忽会导致代码编译通过,但预览界面发生崩溃的情况。

    下面,我们就来看一下到底是怎么回事吧。

    被忽略的“小点”

    首先,来看一下源代码:

    ZStack(alignment: .trailing){
    	HStack {
           VStack {
               Text(Model.shortDateFt.string(from: st.date ?? Date.distantPast))
                   .fontWeight(.light)
                   .font(.footnote)
                   .foregroundColor(.slateGray)
               
               CommonUI.hiveScoringTracesView(st, model: model)
           }.frame(minWidth: 65.0)
            
           stateView
               .foregroundColor(stateColor)
               .frame(minWidth: 30)
            
           HStack(alignment: .top) {
               Text("\(st.totalGainedScore)")
                   .fontWeight(.heavy)
                   .font(.body)
                   .foregroundColor(scoreColor)
                   .opacity(1.0)
               
               baseScoreView
                   .underline(true, color: stateColor)
                   .fontWeight(.bold)
                   .font(.callout)
                   .foregroundColor(stateColor)
           }.frame(minWidth: 50)
           
           HStack {
               
               Text("\(st.sumOfSubScore)")
                   .fontWeight(.bold)
                   .foregroundColor(.mediumPurple)
                   .frame(minWidth: 30)
               
               Text("\(st.ibAddUp)")
                   .fontWeight(.bold)
                   .foregroundColor(.green)
                   .frame(minWidth: 30)
               
               Text("\(st.ipSubtract)")
                   .fontWeight(.bold)
                   .foregroundColor(.red)
                   .frame(minWidth: 30)
               
               Text("\(st.igbAddUp)")
                   .fontWeight(.bold)
                   .foregroundColor(.green)
                   .frame(minWidth: 30)
           }
           .opacity(0.5)
           font(.callout)
           
           if st.hasRemarks{
               Button(action: {
                   withAnimation {
                       self.isUnfoldRemarks.toggle()
                   }
               }){
                   Image(systemName: isUnfoldRemarks ? "arrowtriangle.down.square.fill" : "arrowtriangle.down.square")
                       .foregroundColor(.deepSkyBlue)
                       .font(.system(size: 13, weight: .bold))
                       .offset(x: 15)
               }
               .buttonStyle(BorderlessButtonStyle())
           }
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    上面代码可以顺利通过编译,但会导致Xcode预览崩溃:

    在这里插入图片描述

    喜欢玩“大家来找茬”的小伙伴们可以先尝试找一下是哪里的问题… 😉

    看出问题在哪了吗?

    问题就在于其中一行代码里font()方法前面少了一个点:

    font(.callout)
    
    • 1

    我们的本意是在前面HStack视图上应用字体修改器方法,结果少打了一个点。

    这样默认行为会在self上调用font()方法,那么self是什么呢?

    self就是根视图本身!

    比如,下面self代表的就是MainView视图本身:

    struct MainView: View {
        var body: some View {
            VStack {
                Text("hello world")
                    font(.title)	// 等同于 self.font(.title)
            }
            .padding()
            .frame(width: 200, height: 300)
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    所以,这样写从语法上来说是没问题的,这就是为什么编译可以通过,但预览会崩溃的原因了。

    总结

    有了上面的教训,我们下次在Xcode预览发生崩溃不知所措的时候,记得提醒自己别忘写了那个小小的点哦?😎

    感谢观赏,下次再会!

  • 相关阅读:
    ClickHouse为什么那么快
    求你了,别在高并发场景中使用悲观锁了!
    2022年国赛建模评估
    健康防猝指南1:体重和减肥的秘密
    python opencv把yuv格式转bgr
    web网页设计期末课程大作业:漫画网站设计——我的英雄(5页)学生个人单页面网页作业 学生网页设计成品 静态HTML网页单页制作
    2023.11.13 Spring Bean 的生命周期
    iphone备份后怎么转到新手机,iphone备份在哪里查看
    Windows系统下结束卡死的应用程序
    Python Unittest测试框架
  • 原文地址:https://blog.csdn.net/mydo/article/details/126028769