当前位置:网站首页>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预览发生崩溃不知所措的时候,记得提醒自己别忘写了那个小小的点哦?
感谢观赏,下次再会!
边栏推荐
- Several solutions to spanning
- 【翻译】如何为你的私有云选择一个网络网关
- 数据库优化 理解这些就够了
- Understand the principle behind the virtual list, and easily realize the virtual list
- HCIP第十一天
- [lvgl events] event code
- Using reflection to build a menu spanning tree
- 【服务器数据恢复】HP StorageWorks系列服务器RAID5两块盘离线的数据恢复
- MVC模型:日历系统
- Operator3 - design an operator
猜你喜欢

如何有效进行回顾会议(上)?

天气这么热太阳能发电不得起飞喽啊?喽啊个头……

阿里、京东、抖音:把云推向产业心脏

Leetcode 0142. circular linked list II

Machine learning (Zhou Zhihua) Chapter 6 notes on Support Vector Learning

开源项目丨Taier1.2版本发布,新增工作流、租户绑定简化等多项功能

How to effectively conduct the review meeting (Part 1)?

Security assurance is based on software life cycle -istio authentication mechanism

Leetcode 0143. rearrange linked list

Metersphere -- Open Source continuous testing platform
随机推荐
How to write test cases in software testing technology
Niuke multi school link with level editor i- (linear DP)
Qt5 development from introduction to mastery -- the first overview
[server data recovery] HP StorageWorks series server RAID5 offline data recovery of two disks
软件测试技术之如何编写测试用例
QQ robot configuration record based on nonebot2
文献阅读(245)Roller
离散对数问题(DLP) && Diffie-Hellman问题(DHP)
Custom Configuration Sections
目标检测:速度和准确性比较(Fater R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)
Multithreading and high concurrency (III) -- source code analysis AQS principle
多级缓存方案
Several solutions to spanning
On websocket
Master several common sorting - Select Sorting
Mobile phone scrolling screenshot software recommendation
MiniTest--小程序自动化测试框架
【服务器数据恢复】HP StorageWorks系列服务器RAID5两块盘离线的数据恢复
Thesis study -- masked generative disintegration
修订版 | 目标检测:速度和准确性比较(Faster R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)...