It’s difficult to print to the console using SwiftUI Previews.
There is a tab on the console called Previews, but for me it only seems to work when I run the previews on a physical device. When I’m working on the Live Preview inside Xcode, I still want to be able to print values to a console.
I’m going to be making use of LocalConsole, so make sure you go to File > Add Packages… and paste the following into the search box in the top right:
https://github.com/duraidabdul/LocalConsole
Once you’ve added the package to your project, you should be able to import it into any file.
LocalConsole displays a little console that can be dragged to one of the corners of the screen or resized, making it great for situations when the Xcode debugger isn’t connected.
I’m going to start by determining whether the app is running on Xcode’s SwiftUI Previews. It’s possible to check for an environment variable called XCODE_RUNNING_FOR_PREVIEWS
, but this would still include SwiftUI Previews being run on a physical device. This is notably different from when the app is built and run on a Simulator, as that does not involve SwiftUI Previews at all.
I’m determining whether a Simulator is being used, and when it isn’t, I am returning false.
If you don’t mind LocalConsole running on a device, simply remove all lines beginning with the #
symbol as well as the return false
line.
Next I have my own version of the print
function.
Usually it’s possible to shadow the default print function, meaning you can create a custom version of it at global scope that effectively overrides the default implementation. Unfortunately this approach does not work with SwiftUI Previews as it seems to cause an error.
Instead I removed the terminator parameter from the function, which changes the function signature enough so that it is no longer shadowing.
I’ve made it easier to access the shared instance of LocalConsole’s manager too.
Now I can create my user interface. I am going to be printing whatever is entered into a TextField
when a Button
is pressed. Usually I use Form
, but that presents a problem with the default way LocalConsole is displayed in the top corner means it would cover up the content.
Instead I’m using a simple VStack
that will centre the content.
The only other thing going on here is I’m making the console visible, but only when running on SwiftUI Previews. If you want the console to always be visible, you can simply put these lines directly in the onAppear
block without using an if statement.
Last Week’s Daily Coding Tips
Create a Unix Timestamp to Date Converter With SwiftUI
Unix operating systems originally had a system time that was based on a number of seconds since midnight on January 1st 1970. Every Apple operating system has at its core a Unix kernel called Darwin. That’s why Date().timeIntervalSince1970 exists. Sometimes it’s useful to be able to convert between Unix time and regular …
Make a World Clock App That Converts Between Time Zones
It’s pretty easy to access what time zones the system is aware of by accessing TimeZone.knownTimeZoneIdentifiers. It would probably be more useful if it was an array of TimeZone values, since these have identifier as a property but can also be used in a