显示一些视图,仅当 iOS 14、SwiftUI

Show some view, only if iOS 14, SwiftUI

我想显示文本视图说:

Text("Welcome to iOS 14")

如果用户正在使用 iOS 14.

如果用户使用其他 iOS 版本我想显示不同的文本视图,请说:

Text("Welcome to Different iOS version") 

我怎样才能做到这一点?

struct ContentView: View {
    
    var systemVersion = UIDevice.current.systemVersion

    var body: some View {
        if systemVersion.starts(with: "14" ) {
            Text("welcome to ios 14")
        } else {
            Text("Welcome to Different iOS version")
        }
        
    }
}

这也会查看版本 14.1、14.2 等...

Return 一个字符串,您可以添加任意数量的房屋。你可以把这个概念应用到任何事情上。

struct ContentView: View {  
  
    public var systemVersion: String? {
        return UIDevice.current.systemVersion // "14.2" in my case
    }
    
    var getMessageSystem: String {
        guard let versionOS = Double(systemVersion!)
            else { return "Not a valid iOS version" }
        switch versionOS {
            case _ where versionOS > 14: return "welcome to iOS 14"
            case _ where versionOS > 13: return "welcome to iOS 13"
            case _ where versionOS > 12: return "welcome to iOS 12"
            default: return "welcome"
        }
    }
    
    
    var body: some View {
        Text(getMessageSystem) // welcome to iOS 14
    }
}