当前位置:网站首页>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预览发生崩溃不知所措的时候,记得提醒自己别忘写了那个小小的点哦?
感谢观赏,下次再会!
边栏推荐
- Discrete logarithm problem (DLP) & Diffie Hellman problem (DHP)
- Clickhouse distributed cluster construction
- 【Try to Hack】HFish蜜罐部署
- Four ways to create thread pools
- Qt5 development from introduction to mastery -- the first overview
- Install mysql5.7.36 in CentOS
- As a programmer, how to manage time efficiently?
- Using reflection to build a menu spanning tree
- HCIP第十天
- 走进音视频的世界——FLV视频封装格式
猜你喜欢

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

Leetcode 105. construct binary tree from preorder and inorder traversal sequence & 106. construct binary tree from inorder and postorder traversal sequence

这3款在线PS工具,得试试

MVC模型:日历系统

Istio IV fault injection and link tracking

Qt5 development from introduction to mastery -- the first overview

Clickhouse架构与设计

什么是自旋锁 自旋锁是指当一个线程尝试获取某个锁时,如果该锁已被其他线程占用,就一直循环检测锁是否被释放,而不是进入线程挂起或睡眠状态。 /** * 为什么用自旋锁:多个线程对同一个变量

Metersphere -- Open Source continuous testing platform

Security assurance is based on software life cycle -istio authorization mechanism
随机推荐
QT self-made soft keyboard is the most perfect and simple, just like its own virtual keyboard
软件测试工程师的职业规划
【服务器数据恢复】HP StorageWorks系列服务器RAID5两块盘离线的数据恢复
【Utils】JsonUtil
Redis sentinel mechanism
QQ robot configuration record based on nonebot2
【Utils】CookieUtil
How to effectively conduct the review meeting (Part 1)?
解决uniapp微信小程序canvas不能引入字体的问题
Recommended super easy-to-use mobile screen recording software
How to effectively conduct the review meeting (Part 1)?
Solve the problem that uniapp wechat applet canvas cannot introduce fonts
如何有效进行回顾会议(上)?
Security assurance is based on software life cycle -istio authentication mechanism
Database optimization understanding these is enough
Detailed explanation of C language student achievement management system [easy to understand]
[ecmascript6] set and map
Machine learning (Zhou Zhihua) Chapter 6 notes on Support Vector Learning
Intersectionobserver
开源项目丨Taier1.2版本发布,新增工作流、租户绑定简化等多项功能