Im new to programming and I joined StackOverflow today. I hope I can ask based on the guidelines:
I’m starting SwiftUI and it seems it has many code that works only for iOS 17, take a look at this:
if #available(iOS 17.0, *) {
Text("Hello World")
.font(.title.bold())
.multilineTextAlignment(.leading)
// This modifier only works on iOS 17
.foregroundStyle(.red)
} else {
Text("Hello World")
.font(.title.bold())
.multilineTextAlignment(.leading)
.foregroundColor(.red)
}
One of the first thing I learned is to not repeat the same code but how can I apply different modifiers easily without repeating the code? some thing like this:
Text("Hello World")
.font(.title.bold())
.multilineTextAlignment(.leading)
if #available(iOS 17.0, *) {
.foregroundStyle(.red)
} else {
.foregroundColor(.red)
}
P.S: I have seen posts like this to implement a custom modifier that takes a bool as input, but it can’t take the os version in a way that the compiler understands like #available(iOS 17.0, *).
Thanks for considering my question




