当前位置:网站首页>When Xcode writes swiftui code, it is a small trap that compiles successfully but causes the preview to crash
When Xcode writes swiftui code, it is a small trap that compiles successfully but causes the preview to crash
2022-07-28 14:25:00 【Giant panda Hou Pei】

summary
In the use of Xcode To write SwiftUI Code , A small oversight will cause the code to compile , But the preview interface crashed .
below , Let's see what's going on .
Ignored “ Smaller ”
First , Take a look at the source code :
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())
}
}
}
The above code can be compiled smoothly , But it will lead to Xcode Preview crash :

Like to play “ Spot The Differences! ” My friends can first try to find out what the problem is …
See what the problem is ?
The problem is in one line of code font() A point is missing in front of the method :
font(.callout)
Our intention is to be in front HStack Apply the font modifier method to the view , As a result, a dot was missed .
In this way, the default behavior will be self On the call font() Method , that self What is it? ?
self Is the root view itself !
such as , below self It stands for MainView View itself :
struct MainView: View {
var body: some View {
VStack {
Text("hello world")
font(.title) // Equate to self.font(.title)
}
.padding()
.frame(width: 200, height: 300)
}
}
therefore , Grammatically speaking, it's OK to write like this , This is why compilation can pass , But the reason why the preview will crash .
summary
With the above lessons , Next time we'll be in Xcode When the preview crashes and is at a loss , Remember to remind yourself not to forget to write that little dot ?
Thanks for watching , See you later !
边栏推荐
- [ecmascript6] iterator and generator
- [ecmascript6] set and map
- UI开发中所遇到的各种坑
- 【LeetCode】1331. 数组序号转换
- [util] redis tool class: change the value serializer of redis to genericjackson2jsonredisserializer, and the return value can be object or collection
- C语言中浮点数据类型(你学废了吗)
- 7.27 simulation summary
- 多所“双一流”大学,保研预报名启动!
- Metersphere -- Open Source continuous testing platform
- Three methods to disassemble the rotation array
猜你喜欢

MySQL development skills - View

解决uniapp微信小程序canvas不能引入字体的问题

HCIP第十二天

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

Minitest -- applet automation testing framework

文献阅读(245)Roller

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

Read how to deploy highly available k3s with external database

Leetcode 0142. circular linked list II

QT self-made soft keyboard is the most perfect and simple, just like its own virtual keyboard
随机推荐
Read how to deploy highly available k3s with external database
LeetCode 1331.数组序号转换
HCIP第十二天
Forage QR code -- online QR code generator
Detailed explanation of common commands of vim (VIM use tutorial)
Foundation of deep learning ---- GNN spectral domain and airspace (continuous improvement, update and accumulation)
分集技术简略
Verification code brute force cracking test [easy to understand]
83.(cesium之家)cesium示例如何运行
树莓派基础 | 总结记录树莓派学习过程中的一些操作
jenkins
一些企业数据平台建设的思考
QT自制软键盘 最完美、最简单、跟自带虚拟键盘一样
Daily question - Scholarship
[translation] salt companies come to linkerd for load balancing, and stay for efficiency, reliability and performance
Clickhouse架构与设计
这3款在线PS工具,得试试
Clickhouse architecture and design
解决uniapp微信小程序canvas不能引入字体的问题
Websocket chat