Home > Software engineering >  How to use if #available() in Main
How to use if #available() in Main

Time:11-03

In the code below I want to use .windowResizability only if #available(macOS 13.0, *) == true or @ available(macOS 13.0, *) cause it doesn't available under macOS 13. I can not find the solution by myself.

    import SwiftUI
    
    @main
    struct MyApp: App {
        var body: some Scene {
            DocumentGroup(newDocument: MyDocument()) { file in
                ContentView(document: file.$document)
            }
            // @available(macOS 13.0, *) // <- DOESN'T WORK!
            //.windowResizability(.contentSize) // Only for macOs 13 
        }
    }

CodePudding user response:

In the case of a single expression only you can remove return:

Try this:

struct MacDemoApp: App {
    var body: some Scene {
        
        if #available(macOS 13, *) {
            return DocumentGroup(newDocument: MyDocument()) { file in
                ContentView(document: file.$document)
            }.windowResizability(.contentSize)
        } else {
            return DocumentGroup(newDocument: MyDocument()) { file in
                ContentView(document: file.$document)
            }
        }
    }
}

CodePudding user response:

Just wrap it with if statement

if #available(macOS 13, *) {
    .windowResizability(.contentSize)
}
  • Related