BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

Helper method to convert view to Image now returns warning

Forums > SwiftUI

The helper method to convert a SwiftUI view to an image from here: https://www.hackingwithswift.com/quick-start/swiftui/how-to-convert-a-swiftui-view-to-an-image

With Xcode 14, this method sometimes returns a warning "Result of call to function returning 'Bool?' is unused".

The warning is tied to the return line: view?.drawHierarchy(in: controller.view.bounds, afterScreenUpdates: true)

The warning only appears sometimes - I'd say in every second build.

Why is this warning happening, and how to fix it?

   

drawHierarchy returns a Bool. When you don't need the return value then

_ = view?.drawHierarchy(in: controller.view.bounds, afterScreenUpdates: true)

should fix this. Why it doesn't show the warning everytime I don't know. Perhaps a bug in Xcode.

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.