xcode 如何在DocumentGroup应用程序的Main中使用if #available()

hfwmuf9z  于 2022-11-18  发布在  其他
关注(0)|答案(2)|浏览(142)

在下面的代码中,我想使用.windowResizability仅当#available(macOS 13.0,)== true或@ available(macOS 13.0,)时,因为它在macOS 13下不可用。我自己找不到解决方案。

//
//  Test_HowAviableApp.swift
//  Test HowAviable
//
//  Created by Sebastien REMY on 03/11/2022.
//

import SwiftUI
import UniformTypeIdentifiers

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

struct MyDocument: FileDocument, Codable {
    
    static var readableContentTypes = [UTType(exportedAs:"com.test.test")]
    var test = "test"
    init() {
        
    }
    
    init(configuration: ReadConfiguration) throws {
        if let data = configuration.file.regularFileContents {
            self = try JSONDecoder().decode(MyDocument.self, from: data)
        }
    }
    
    func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
        let data = try JSONEncoder().encode(self)
        return FileWrapper(regularFileWithContents: data)
    }
}

struct MyView: View {
    @Binding var document: MyDocument
    
    var body: some View {
        Text("Hello")
    }
}
pxq42qpu

pxq42qpu1#

在只有单个表达式的情况下,可以删除return
试试看:

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)
            }
        }
    }
}
qmelpv7a

qmelpv7a2#

只要用if语句 Package 即可

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

相关问题