@available function decorator not working on macOS

183 Views Asked by At

The following function converts the pages of a pdf document into NSImages.

    @available(macOS 13.0, *)
func convertPDFPagesToImages(pdfDoc: PDFDocument)-> [NSImage] {
    
    let pageCount = pdfDoc.pageCount
    var images: [NSImage] = []
    for i in 0..<pageCount {
        let page = pdfDoc.page(at: i)
        let pdfKitView = PDFKitView(pdfDoc: pdfDoc, viewSize: CGSize(width: 600, height: 814), currentPage: .constant(page))
        let renderer = ImageRenderer(content: pdfKitView)
        if let cgImage = renderer.cgImage {
            let image = NSImage(cgImage: cgImage, size: NSSize(width: 600, height: 814))
            images.append(image)
        }
        
    }
    
    return images
}

Error: Cannot find 'ImageRenderer' in scope.

I have imported SwiftUI. According to Apple's documentation, Image renderer should be available from macOS 13. Is this a bug or have I misunderstood the @available decorator?

1

There are 1 best solutions below

1
Sweeper On

Please check your Xcode version. Xcode 14.1 is the first version to support macOS 13 development. Xcode 14.0 only has support for macOS 12.3. So on Xcode 14.1+, your code should compile fine.

On the other hand, Xcode 14.0 does support iOS 16.0, so it is possible to use ImageRenderer on the iOS platform.

You are free to write any reasonable-looking version in the @available attribute, but if you don't have the SDK, you of course cannot use anything from that version.