Cannot print to console in Xcode - ios

I am new to Xcode, macOS development etc.. So maybe its just because I am new - but I could not make a simple printout to console work with all effort.
I created a minimum nonworking example of my problem:
import SwiftUI
struct Test: View {
#State var message = "Test"
var body: some View {
Button(action: {
print("test worked")
message = "test worked"
}) {
Text(message)
}
}
}
struct Test_Previews: PreviewProvider {
static var previews: some View {
Test()
}
}
When executing this the Button text changes, but nothing appears in console.
I tried by following this tutorial, but it did not work with it either: https://www.hackingwithswift.com/read/18/2/basic-swift-debugging-using-print
I tried by enabling debug preview according to this thread: How to print() to Xcode console in SwiftUI?
And I tried by enabling this setting:
Nothing helps.. I am using Xcode Version 12.1 (12A7403) btw.

You cannot print to the console from a SwiftUI preview.
The only possibility for outputting debug info in a preview is to display your logs in a Text (or any other UI element) that's displayed in your Preview.
However, if you need proper debugging, run the full app, don't use previews. Previews are great for initial wireframes, but once you get to the stage where you need debugging, switch to using the view in your app and running that on the Simulator (or a real device) rather than using the preview.

I have faced the same problem nothing shows in console.This helps me. Make sure you mark right side button to see the debug console.

It's possible to debug SwiftUI previews and print in the console without launching the app on a device or simulator. From the canvas, make sure to click on "Debug preview". More info can be found on Apple website.
Xcode 12
Long press on the Live Preview button, then click on Debug Preview.
Xcode 11
Right-click (or Control-click) on the Live Preview button in the bottom right corner of the preview.

I'm also having the same problem. I think it's a bug. I am pretty sure it was working on some previous xcode versions.
Anyway as a workaround you can just set a breakpoint on the line where you 'd like to log something and manually set a debugger command action like you can see in the picture.
Don't forget to tick the checkbox if you want to just print the log without actually stopping.

Related

Why is Xcode crashing when I try to preview?

Every time I am trying to resume my preview canvas in Xcode I get this annoying error. I tried restarting, moving the project to another location, and changing the preview device. The current project is a fresh one, just started building a view.
Strange thing is that when I run it on the iOS Simulator it works. The app builds successfully. I also noticed that this is only happening when I use source control with my project(GitHub). Not using it is not an option for me.
Let me know if I need to add the whole crash report, because it's very long.
Here is the full crash log
https://developer.apple.com/forums/content/attachment/5f8f5c96-7c1e-4eef-b0d7-ed59894a9c30
You didn't add your code so I can't help specifically but here are some of the reasons it happened to me in the past:
If you use an environment object on the view you're previewing and you don't add it to preview, it causes a crash:
struct MyView_Previews: PreviewProvider {
static var previews: some View {
MyView()
.environmentObject(ViewModel())
}
}
Sometimes I just add ZStack to it and it magically solves the problem:
struct MyView_Previews: PreviewProvider {
static var previews: some View {
ZStack {
MyView()
}
}
}
Sometimes It helps to clean the project (command + shift + K) and build again
Basically if your project works in simulator it means there's nothing wrong with it, it builds. The only problem is that you can't use the preview. You probably didn't set it right, it's missing something it needs in order to run properly. Try to figure it out. Or just run on simulator like we did before there was such a thing as previews..

How to print to the Debug Area with Swift UI in Xcode 13?

I want my print message to be shown in the Debug Area in Xcode 13. There are several questions like this on StackOverflow, some of them suggest using the Debug Mode, which seems to have been removed in Xcode 13.
Here is my ContentView:
struct ContentView: View {
var body: some View {
Button("button", action: {
print("abc")
})
}
}
Though I use print("abc"), when I click the button, there's nothing in the Debug Area. How to show it?
The problem is that you're not running the app. You're just looking at the preview. Press Command-R and run the app in a simulator; tap the button in the simulator.
Note that when you're actually running the app, the debug bar (at the top of the debug area) looks quite different:

SwiftUI TabView not responding

:-) Here comes a probably silly problem with a bit of a preamble sorry!
Long story short - I've been developing an app pulling my hair out due to a bug with the TabView in SwiftUI. I was overlaying the tabs with a ZLayer to add a funky big button in the middle - so when I was running into problems I thought this would be the issue... but this becomes irrelevant in a moment.
For simplicity I started a New project in XCode v11.6, build target iOS 13.6, Selected Tabbed App, created a very simple boilerplate SwiftUI content view with 2 tabs. (For clarity - I did not modify the default code at all). Runs perfectly on the simulator. Plug in my iPhone XS running iOS 13.6.1 (when I first encountered this problem I was on 13.5 so have updated iOS builds whilst this problem has occurred). Run the simple, non modified app on my phone. Loads up "First View", I tap the "Second" tab, nothing. No response at all. This is the same problem I am having in my more complicated app. Occasionally if I keep rebuilding. It will let me switch to "Second View" but then cannot switch back. The Tab Controller just seems to become non-responsive.
Now back to my more complicated app, on the first view there is a scroll view and there is a button with an attached actionSheet. If I pop open the action sheet, then dismiss it (regardless of just calling the .cancel() button or selecting an action), the Tab Controller works perfectly. There is another option which opens another sheet, again as soon as something has been overlayed, everything works as expected until the next app launch.
Tried resetting my MacBook, tried resetting my iPhone, tried uninstalling the build then rebuilding, cleaning the build folder, tried creating multiple projects with build targets iOS 13, 13.2, 13.5, 13.6. Problem seems to persist, but every time the simulator works perfectly.
So my questions here are: From my searching I can't find anyone with this problem. Is it just me really? Can anyone with XCode 11.6 and an iPhone XS please simply hit "New Project" -> "Tabbed App" and let me know if this works? (I'm almost wondering if there's a bug in XCode 11.6 when you create an app and everyone is either working on projects that were already created pre XCode 11.6 or already using the beta XCode... But really I am just out of ideas)
Also is there any deeper debugging one can do? Something like actually notifying me for every touch it receives and whether the App sandbox is getting the touch or the OS is (for some reason) keeping the tap gesture? I've never experienced something like this so I can't really see where to start debugging as I don't even know if the app is receiving the tap.
Or frankly - does anyone have any other ideas?
Just for clarity - ContentView looks like this: (Again it's straight from what XCode creates for a Tabbed App)
import SwiftUI
struct ContentView: View {
#State private var selection = 0
var body: some View {
TabView(selection: $selection){
Text("First View")
.font(.title)
.tabItem {
VStack {
Image("first")
Text("First")
}
}
.tag(0)
Text("Second View")
.font(.title)
.tabItem {
VStack {
Image("second")
Text("Second")
}
}
.tag(1)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Thanks in advance and sorry for the long ramble - from a very sleepy dev at wits' end!

How to print the hierarchy of views in iOS? [duplicate]

Is there a GUI tool that inspects the view hierarchy of an iOS app? I'm thinking about Webkit's web inspector or similar tools. I'm looking to debug layout issues, like views having the wrong position or size, or a child not being properly contained in its parent. Currently I have to add asserts that test these various conditions by hand, or set different background colours on different views, and as you can imagine, that's a really tedious way to go about it.
I looked at Instruments's UI recorder, but that only records and plays back UI actions` and, in any case, works only for Mac apps.
Is there a better solution?
I don't know if there is a GUI view inspection tool, but I have had some luck with the debugging method on UIView: -recursiveDescription
if you pause the program in the debugger and input this into GDB (Edit: also works in LLDB)
po [[UIWindow keyWindow] recursiveDescription]
You get a printout of your entire view hierarchy. You can also call it on a specific view to get a printout of the view hierarchy of that view.
It can be a little tedious to wade through the info you get out of it, but it has proved useful to me.
Credit goes to this blog post which talked about this method and also linked to this helpful, but rather hard to find Apple tech note.
Xcode 6 now has 3D view hierarchy inspection built in like Reveal App and Spark Inspector.
Click on the "Debug View Hierarchy" button while your app is running to pause execution and inspect the views at the current moment.
More info at Apple's documentation.
Oddly enough, now there is another option, http://revealapp.com/, which as of this post is in an open (free) beta. As you can see it's another visual inspector.
EDIT 2014-04-05: Reveal is out of Beta and no longer free. There is a 30-day trial, however.
This question is old but let me put info here about new tool which I develop:
https://github.com/glock45/iOS-Hierarchy-Viewer
Just to keep this thread up to date, I've been recently playing with Spark Inspector. It's not free, but it's very nice.
The FLEX Debugger provides an in app view inspector that allows you to modify the UI in a running app. It also logs network requests.
https://github.com/Flipboard/FLEX
Free : Just type this in inspector :
po [[UIWindow keyWindow] recursiveDescription]
Commercial : http://revealapp.com/ I tested beta version of revealapp, it was good though has bugs.
Another Commercial tool : http://sparkinspector.com/ it's working seamless.
Swift 4.
iOS:
extension UIView {
// Prints results of internal Apple API method `recursiveDescription` to console.
public func dump() {
Swift.print(perform(Selector(("recursiveDescription"))))
}
}
macOS:
extension NSView {
// Prints results of internal Apple API method `_subtreeDescription` to console.
public func dump() {
Swift.print(perform(Selector(("_subtreeDescription"))))
}
}
Usage (in debugger): po myView.dump()
This dumps all in debug window.(Hard to read tho) :(
Working on iOS 10, Xcode 8.3.3
po UIApplication.shared.keyWindow?.recursiveDescription()
For swift/Xcode 10, enter this into the debug console:
po yourView.value(forKey: "recursiveDescription")!
It will print out a recursive hierarchy for any given UIView.
(Credit: How to debug your view hierarchy using recursiveDescription)
The approved answer no longer works for me, using Xcode 8 and Swift 2.3. Here's what does work for me:
po UIApplication.sharedApplication().keyWindow?.recursiveDescription()

Traversing the View Hierarchy on an iPhone or iPad [duplicate]

Is there a GUI tool that inspects the view hierarchy of an iOS app? I'm thinking about Webkit's web inspector or similar tools. I'm looking to debug layout issues, like views having the wrong position or size, or a child not being properly contained in its parent. Currently I have to add asserts that test these various conditions by hand, or set different background colours on different views, and as you can imagine, that's a really tedious way to go about it.
I looked at Instruments's UI recorder, but that only records and plays back UI actions` and, in any case, works only for Mac apps.
Is there a better solution?
I don't know if there is a GUI view inspection tool, but I have had some luck with the debugging method on UIView: -recursiveDescription
if you pause the program in the debugger and input this into GDB (Edit: also works in LLDB)
po [[UIWindow keyWindow] recursiveDescription]
You get a printout of your entire view hierarchy. You can also call it on a specific view to get a printout of the view hierarchy of that view.
It can be a little tedious to wade through the info you get out of it, but it has proved useful to me.
Credit goes to this blog post which talked about this method and also linked to this helpful, but rather hard to find Apple tech note.
Xcode 6 now has 3D view hierarchy inspection built in like Reveal App and Spark Inspector.
Click on the "Debug View Hierarchy" button while your app is running to pause execution and inspect the views at the current moment.
More info at Apple's documentation.
Oddly enough, now there is another option, http://revealapp.com/, which as of this post is in an open (free) beta. As you can see it's another visual inspector.
EDIT 2014-04-05: Reveal is out of Beta and no longer free. There is a 30-day trial, however.
This question is old but let me put info here about new tool which I develop:
https://github.com/glock45/iOS-Hierarchy-Viewer
Just to keep this thread up to date, I've been recently playing with Spark Inspector. It's not free, but it's very nice.
The FLEX Debugger provides an in app view inspector that allows you to modify the UI in a running app. It also logs network requests.
https://github.com/Flipboard/FLEX
Free : Just type this in inspector :
po [[UIWindow keyWindow] recursiveDescription]
Commercial : http://revealapp.com/ I tested beta version of revealapp, it was good though has bugs.
Another Commercial tool : http://sparkinspector.com/ it's working seamless.
Swift 4.
iOS:
extension UIView {
// Prints results of internal Apple API method `recursiveDescription` to console.
public func dump() {
Swift.print(perform(Selector(("recursiveDescription"))))
}
}
macOS:
extension NSView {
// Prints results of internal Apple API method `_subtreeDescription` to console.
public func dump() {
Swift.print(perform(Selector(("_subtreeDescription"))))
}
}
Usage (in debugger): po myView.dump()
This dumps all in debug window.(Hard to read tho) :(
Working on iOS 10, Xcode 8.3.3
po UIApplication.shared.keyWindow?.recursiveDescription()
For swift/Xcode 10, enter this into the debug console:
po yourView.value(forKey: "recursiveDescription")!
It will print out a recursive hierarchy for any given UIView.
(Credit: How to debug your view hierarchy using recursiveDescription)
The approved answer no longer works for me, using Xcode 8 and Swift 2.3. Here's what does work for me:
po UIApplication.sharedApplication().keyWindow?.recursiveDescription()

Resources