当前位置:网站首页>Xcode编写SwiftUI代码时一个编译通过但导致预览(Preview)崩溃的小陷阱
Xcode编写SwiftUI代码时一个编译通过但导致预览(Preview)崩溃的小陷阱
2022-07-28 13:25:00 【大熊猫侯佩】

概述
在使用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())
}
}
}
上面代码可以顺利通过编译,但会导致Xcode预览崩溃:

喜欢玩“大家来找茬”的小伙伴们可以先尝试找一下是哪里的问题…
看出问题在哪了吗?
问题就在于其中一行代码里font()方法前面少了一个点:
font(.callout)
我们的本意是在前面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)
}
}
所以,这样写从语法上来说是没问题的,这就是为什么编译可以通过,但预览会崩溃的原因了。
总结
有了上面的教训,我们下次在Xcode预览发生崩溃不知所措的时候,记得提醒自己别忘写了那个小小的点哦?
感谢观赏,下次再会!
边栏推荐
- 修订版 | 目标检测:速度和准确性比较(Faster R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)...
- 7.27模拟赛总结
- RSA encrypts data with private key and decrypts data with public key (not a signature verification process)
- Clickhouse架构与设计
- 多级缓存方案
- 什么是自旋锁 自旋锁是指当一个线程尝试获取某个锁时,如果该锁已被其他线程占用,就一直循环检测锁是否被释放,而不是进入线程挂起或睡眠状态。 /** * 为什么用自旋锁:多个线程对同一个变量
- jenkins
- 文献阅读(245)Roller
- webSocket聊天
- 【Utils】FastDFS工具类
猜你喜欢

bgp实验

UFIDA BiP CRM new product launch enables large and medium-sized enterprises to grow their marketing

These three online PS tools should be tried

Discrete logarithm problem (DLP) & Diffie Hellman problem (DHP)
![[ecmascript6] set and map](/img/64/dd6ffc5f0faf881b990e609cf62343.png)
[ecmascript6] set and map

83. (cesium home) how the cesium example works

Jmeter安装教程及登录增加token

MeterSphere--开源持续测试平台

LeetCode 105.从前序与中序遍历序列构造二叉树 && 106.从中序与后序遍历序列构造二叉树

zabbix分布式
随机推荐
Understand BFC features and easily realize adaptive layout
Install mysql5.7.36 in CentOS
Master closures and consolidate basic skills
[server data recovery] HP StorageWorks series server RAID5 offline data recovery of two disks
在centos中安装mysql5.7.36
MeterSphere--开源持续测试平台
MySql5.5之后的默认存储引擎为InnoDB。
How to write test cases in software testing technology
Redis sentinel mechanism
A label_ File download (download attribute)
【Utils】CookieUtil
Understand the principle behind the virtual list, and easily realize the virtual list
Literature reading (245) roller
Four ways to create thread pools
天气这么热太阳能发电不得起飞喽啊?喽啊个头……
【Utils】JsonUtil
83. (cesium home) how the cesium example works
开源项目丨Taier1.2版本发布,新增工作流、租户绑定简化等多项功能
Do you really know esmodule
TS literacy method - Basic chapter