3

There is an ability to run code related to version of OS:

if #available(macOS 11.0, *) {
    Button("test", action: {})
         //.modifier1
         //.modifier2
         //.modifier3
         //.modifier4
         //.modifier5
         //.modifier6
         //.modifier7
         //.modifier8
         .foreground(.red)
} else {
    Button("test", action: {})
         //.modifier1
         //.modifier2
         //.modifier3
         //.modifier4
         //.modifier5
         //.modifier6
         //.modifier7
         //.modifier8
}

Is it possible to do the same on single modifier without duplication of code?

Sth like syntax:

Button("test", action: {})
     //.modifier1
     //.modifier2
     //.modifier3
     //.modifier4
     //.modifier5
     //.modifier6
     //.modifier7
     //.modifier8
    .if(macOS 11.0, *) { $0.foreground(.red) }

?

Andrew_STOP_RU_WAR_IN_UA
  • 9,318
  • 5
  • 65
  • 101

3 Answers3

3

Here is what you are looking for:

struct ContentView: View {
    
    var macOS_11: Bool {
        if #available(macOS 11.0, *) { return true }
        else { return false }
    }
    
    var body: some View {
        
        Text("Hello, World!")
            .foregroundColor(macOS_11 ? .red : nil)
        
    }
    
}

Version 2.0.0:

Here is better way:

struct ContentView: View {
    
    var body: some View {
        
        VStack {
            
            Text("Hello, World!")
                .foregroundColor(MacOsVer.shared.higherThan_11 ? .red : nil)
            
            
            Text("Hello, World!")
                .foregroundColor(MacOsVer.shared.higherThan_11_3 ? .red : nil)
            
            
            Text("Hello, World!")
                .foregroundColor(MacOsVer.shared.higherThan_12 ? .red : nil)
            
        }
        .frame(width: 200)
        .padding()
 
    }
    
}


public class MacOsVer {
    
    let higherThan_11: Bool
    let higherThan_11_3: Bool
    let higherThan_12: Bool
    
    init() {
        
        if #available(macOS 11.0, *) { higherThan_11 = true }
        else { higherThan_11 = false }
        
        if #available(macOS 11.3, *) { higherThan_11_3 = true }
        else { higherThan_11_3 = false }
        
        if #available(macOS 12, *) { higherThan_12 = true }
        else { higherThan_12 = false }
        
    }
    
    static let shared: MacOsVer = MacOsVer()
    
}
ios coder
  • 1
  • 4
  • 31
  • 91
  • 1
    I have based my solution on your answer, and have added the little bit changed own version of your code. Thanks for your help and inspiration :) But your answer is still your :) – Andrew_STOP_RU_WAR_IN_UA Oct 31 '21 at 09:42
1

You could try something like this:

struct ContentView: View {
    let macOS11Available: Bool
    
    init() {
        if #available(macOS 11, *) {
            self.macOS11Available = true
        } else {
            self.macOS11Available = false
        }
    }
    
    var body: some View {
        VStack (spacing: 77) {
            Button("test modifier", action: {})
                .foregroundColor(macOS11Available ? .red : .green)
            //.modifier1
            //.modifier2
            //.modifier3
            //.modifier4
            //.modifier5
            //.modifier6
            //.modifier7
            //.modifier8
            
        }.frame(width: 444, height: 444)
    }
}
  • @Andrew: I am not big fan of using `init` of a View! It would get initialized so many time! Even in my answer you can bring `macOS_11` out of view and change it as public and global value and in this case you would read macOS version just 1 time vs 1000 times in app running life time. PS: It is better to change computed value to a variable for more better coding. – ios coder Oct 31 '21 at 09:38
  • 1
    @swiftPunk I have based my solution on your answer, and have added the own version of your code. Thanks for your help and inspiration :) – Andrew_STOP_RU_WAR_IN_UA Oct 31 '21 at 09:41
1

Solution based on idea of swiftPunk

public class MacOsVer {
    static var higherThan_11: Bool {
        if #available(macOS 11.0, *) { return true }
        else { return false }
    }
    
    static var higherThan_11_3: Bool {
        if #available(macOS 11.3, *) { return true }
        else { return false }
    }
    
    static var higherThan_12: Bool {
        if #available(macOS 12, *) { return true }
        else { return false }
    }
}

So with if modifier ( https://prnt.sc/1xx7pir )

I can do sth like:

Text("some Text")
    .bacground(.blue)
    .if(MacOsVer.higherThan_11) { $0.foregroundColor(.Red) }
Andrew_STOP_RU_WAR_IN_UA
  • 9,318
  • 5
  • 65
  • 101