How do I debug my iOS app with Swift - ios

I started building my first iOS app and I am trying to debug it. I set a break point on my viewDidLoad() function (line 14) like so:
When I run the app and the get the ViewController to load, my debugger view opens in xcode but I am getting this (what looks like assembly code) view.
I can't seem to figure out how to view the debugger in my Swift code and be able to step through it in Swift. Does anyone know how to accomplish this? Thanks!

I found the issue.
Debug > Debug Workflow > "Always Show Disassembly" was checked off. Unchecking this now shows my source code when debugging.

You should set the breakpoint on a line of code inside the method and not the call to the super class.
Then you'll be able to analyze variables at the breakpoint.

Related

Breakpoint stop and shows wrong window in Xcode 8

I've added breakpoint in ViewDidLoad but when code execute and stop on breakpoint then it shows wrong window (Thread Window) instead of in Code.
showing thread window
I've Xcode 8.3.3 and I think this may be some issue in setting or may be something else, kindly guide me where i did wrong ?
Looking for help. Thanks
After hit and try I finally found solution, I did wrong and mistakenly check Allow Show Disassembly.
Allow Show Disassembly should be unchecked.
Hope this will help others too.

iOS TodayView Widget breakpoints not working

I'm working through this tutorial and it works just fine on a simulator, except I don't understand how the methods are being called. The today view widget displays fine but when I add breakpoints to the methods (e.g. ViewDidLoad, widgetPerformUpdateWithCompletionHandler) the breakpoints never seem to be called.
I'm trying to figure this out as I've added extra code - e.g. NSLog to display some values within the methods but do not see any output from the NSLog calls.
Can someone explain why the breakpoints are not working? I'm guessing that it has something to do with the extension methods are executing in the 'background' but am not sure.
Thanks
You are able to have the breakpoints you set in your today extension activate by following the procedure below:
1) Set breakpoint in your extension code (viewDidLoad is a good option to test)
2) Launch your app as you normally would by selecting your app's target and hitting run.
3) Make sure that your extension is installed in the today view (open the today view and hit the edit button to add it if it is not)
4) Close the today view.
5) In Xcode select your today extension target and press the run button. You will be prompted to choose an app to run. Select "Today".
6) You should see the today window appear on the simulator (this also works on the device). Your breakpoint will be hit.
NOTE: You may hit an exception breakpoint in your app prior to the today extension launching because your app is sent to the background. If this happens just skip over the breakpoint and you will hit the breakpoint in your extension as expected. This procedure also allows you to see console statements from your extension.
I'm not sure if this is related to your specific situation, but in an app where I have 3 extensions, I noticed that breakpoints don't work in 2 of them.
I noticed however, that the breakpoints start working if I run (from XCode) the extension on which the breakpoints work, and access one of the other extensions (from inside Photos app in my case). For some reason, running the other extensions from XCode would not trigger the breakpoints.

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()

Is there a way to see which line of code was just executed in Xcode?

I'm debugging an iOS app in Xcode, and I'm wondering is there any way to see which line of code I just ran? I'm trying to figure out where the code is that is responsible for making a menu slide open when I tap the menu key.
Thanks
Set a breakpoint (tap on a line number in Xcode) and manually run the code line by line using Xcode's Debug menu. In your case you can set a breakpoint on the method that is executed when a button is pushed. If you do not know which method is called when you push the button, you could look at where the button is declared, which should have a addTarget method, or see what it is hooked up to in the Storyboard.
If you have a few places where you think the code is executed, you can use NSLog() statements and see what is being printed to the console at what time.

App pausing but not getting an error after running a clean

I have no idea what is after going wrong but I'll state the events that happened before the error occurred
1: App was running fine as I was adding functionality.
2: I noticed that one of the view controllers was not showing the correct version but an older one. (I had added in a segmented control and it wasn't showing up)
3: I tried to uninstall the from my phone and build it again but it was still showing the old controller
4: I tried cleaning the project to recompile everything, after this step the application started to give me this problem
http://i.imgur.com/105yDr8.png
Some extra information:
1: This error occurs on the black screen before any view controllers load
2: The view controller I was working on was not the first view controller (so I do not think that it is the problem)
3: The main view controller has not been changed in days and was working perfectly since it was finished
I have absolutely no idea what is going wrong, and help would be greatly appreciated
UPDATE 1
New error message
http://i.imgur.com/5XVJWHT.png
UPDATE 2
Screenshot of storyboard and a finder window that opened when I clicked "show in finder on the storyboard". This would lead me to believe that it does know where the file is
http://i.imgur.com/Ybas1Yx.png
You've (probably accidentally) set a breakpoint at the return UIApp...-line.
You can identify a breakpoint by it's blue color at the left of a line. If it's activated, it's dark blue, else it's light blue. You can just drag it either to the trash can or somewhere else in your window to completely delete the breakpoint.
your app is getting crashed due to exception check the pannel objc_exception_throw
add exception breakpoint and you'll get the line of code where your app is crashing
It sounds like something is broken, and throws an exception. You're just seeing the exception being caught.
The way to investigate what is throwing is to add an exception breakpoint. Go to the breakpoints list and hit +, choose exception breakpoint and leave it at all. This should give you a breakpoing at the point where the except is being thrown.
As to what is happening. You had an app that ran with an old VC, you deleted the app and re-installed and now it crashes, so best guess is that your old version had the VC, but new one doesn't. Sounds like a missing xib or storyboard. It clearly isn't missing from the project, as you'd see that immediately, but check for target membership, changed name or localization problems.
I fixed the problem by renaming the storyboard to MainStoryboard1 and changing the target in
project > target > summary > iphone/ipod deployment

Resources