如果数字在30以上,则为SwiftUI

如果数字在30以上,则为SwiftUI,swiftui,Swiftui,因此,如果一个数字大于34,我尝试显示一个不同的UI 然而,不知何故,代码在3以上触发。不太清楚为什么它会在>3而不是>34触发 if self.speed > "\(String(34))" { VStack(alignment: .center){ Text("YOUR SPEED IS") .multilineTextAl

因此,如果一个数字大于34,我尝试显示一个不同的UI

然而,不知何故,代码在3以上触发。不太清楚为什么它会在>3而不是>34触发



if self.speed > "\(String(34))" {
                VStack(alignment: .center){
                    Text("YOUR SPEED IS")
                        .multilineTextAlignment(.center)
                        .lineSpacing(30)
                        .padding(20)
                        .font(.system(size: 30, weight: .heavy, design: .default))
                        .foregroundColor(Color.gray)
                                            
                    Text(String(self.speed))
                        .multilineTextAlignment(.center)
                        .lineSpacing(30)
                        .padding(20)
                        .font(.system(size: 50, weight: .heavy, design: .default))
                        .foregroundColor(Color.red)
                                            
                    Text("Our system detected you driving or moving too fast")
                        .multilineTextAlignment(.center)
                        .font(.system(size: 12, weight: .light, design: .default))
                        .foregroundColor(Color.gray)
                        
                    
                    
                }.frame(minWidth: 0,
                        maxWidth: .infinity,
                        minHeight: 0,
                        maxHeight: .infinity,
                        alignment: .topLeading
                ).background(Color.black)
            }
我想知道我的if语句是否正确

我从用户位置获取速度(注意,我需要以公里为单位的速度)

注意,它不允许我将其保存为Int

if Int(self.speed)! > 34 {

不要将
34
转换为字符串,而是将
self.speed
转换为Int或Double


老实说,self.speed一开始应该是Int。

因为您将数字作为字符串进行比较。“4”按字典顺序排在“34”之后。
self.speed
必须是
字符串,否则代码无法编译。你是如何得到这个值的?它可以存储为
Int
Double
吗?@vacawama它不允许我将速度保存为Int或Double这是否回答了您的问题?我将其转换为Int并使其工作。如果自身速度>34
if Int(self.speed)! > 34 {
if Int(self.speed) ?? 0 > 34 {