KnowledgeCity

Swift: Debugging and Testing

Debugging is one of the most overlooked but valuable skills in software development.

Debugging is one of the most overlooked but valuable skills in software development. Proper debugging will get you to your end application faster and with fewer headaches along the way. These skills are essential for Swift developers and will help you deliver more usable apps and programs. The topics discussed in this course will build on your knowledge of the Swift programming language and explain some of the tools offered by Xcode to test and debug your code.

In Swift: Debugging and Testing, you will learn how to test and debug your code in Swift. We will discuss assertions in Swift, which are a way of asserting a statement's truth and testing that assertion. We will also explain how to use breakpoints in your code, which pause Xcode to let you inspect the state of your program and address any possible bugs. We will also look at debugging with capture view hierarchy and view debugging to understand how to find errors in the user interface. 

Learning Objectives

  • Describe debugging in Swift
  • Explain how to debug a view in Swift
  • Identify different unit tests in Swift

Author: Mark Nair

Duration: 24m · 4 lessons
Level: Advanced
Language: English

Skills you’ll gain

Code TestingDebuggingSoftware TestingSymbolic DebuggingUitestUnit Testing

What You'll Learn

  • Describe debugging in the Swift programming language
  • Use assertions to test the truth of a statement in Swift
  • Apply breakpoints to pause Xcode and inspect program state
  • Debug a view using capture view hierarchy and view debugging
  • Identify different unit tests in Swift

Key Takeaways

  • Debugging is an overlooked but valuable skill that helps Swift developers reach their end application faster and with fewer headaches.
  • The course builds on existing Swift knowledge and covers Xcode tools for testing and debugging code.
  • Assertions in Swift are a way of asserting a statement's truth and testing that assertion.
  • Breakpoints pause Xcode so you can inspect the state of your program and address possible bugs.
  • Capture view hierarchy and view debugging help find errors in the user interface.

Frequently Asked Questions

Who is this course for?

It is for Swift developers who want to build on their knowledge of the Swift programming language and learn to test and debug their code, since these skills are essential for delivering more usable apps and programs.

What tools does this course cover?

It explains tools offered by Xcode to test and debug code, including assertions, breakpoints, capture view hierarchy, and view debugging.

What will I learn to do with breakpoints?

You will learn how to use breakpoints in your code to pause Xcode and inspect the state of your program so you can address any possible bugs.

What topics are taught in the lessons?

The lessons cover Debugging With Assert and Breakpoints, Debugging with Capture View Hierarchy, View Debugging, and Unit Testing.

What skills does this course build?

It builds skills in code testing, debugging, software testing, symbolic debugging, UI testing, and unit testing.

Transcript

Show transcript (free preview lesson)

Transcript of the free preview lesson. Remaining lessons unlock with the full course.

Hi, my name is Mark Nair, and in these lessons, you learn about debugging and testing in Swift. We'll go over debugging with assert and break points, debugging with captured view hierarchy, view debugging, and unit testing. Debugging is one of those often overlooked but terrifically valuable skills in development. Property debugging will get you to your end application faster and with fewer headaches along the way. Let's take a look at assert first. I have a new playground here, and what I'm going to do is just put in a few simple lines of code. Now right now, I just have a quick variable or a quick constant called the number, which is equal to 50, and then I have the total, oh, that's why I'm saving the problem. The total is equal to a number right here, and one plus 110. Now it's pretty easy, we just run this and we'll just see that the total is 160. All right, that makes sense, 110 plus 50. Now let's test this out, but without us trying to figure out the total ourselves right here and taking it with the print statement, and to do this, I'll use a technique called assert. So I'm going to assert what I think the truth is of this operation. So I will just say assert, you'll see here in the pop-up we have a condition and then a message. So we have these two qualifiers here. So the condition, I'll just say the total is equal to 150. Now I'll have a message of failure. Now what happens is when the operation runs, and I have the total, right now we know the total is 160, but I'm saying I'm going to assert the total is 150. That should be the truth of this. If not, I want to see a failure statement, and sure enough, I have an error here, bad crash error, but down here in the log, I have assertion failed failure, which is my message right here. Now if I change this to, I'm going to assert 160, which is the truth of a thing, then everything is going to be okay. So assert is just a way that I can put in a value, and if I have some kind of extraordinary long calculation or something strange up here, instead of something easy, I can just assert the end result of that and see what happens. And this is testing it. Now this assert will get us into unit testing, which is coming up. I wanted to show it to you right now inside of playgrounds. And the next thing we're going to do is talk about breakpoints, and that's what we'll use Xcode for the full project version of that, not playgrounds. Okay, here I have a new project that I call breakpoints, very simple project I have an array of colors. So here we go. And then in the body, I'm just using a for each to basically loop through the colors. So here it is, and here's the text color, and there it is in the screen. I have a simulator open over here too, so we can see what happens in the log. At this point of Xcode, you have to run the simulator outside of Xcode in the environment here to see stuff in the log B. The preview over here, the canvas just won't show that to you. All right, so what I want to show you is how a breakpoint works in the logic of the code. So in this area right here, which is called as the gutter, right to the left of say line 12, I will click, and this puts in this little blue thing, and this is a breakpoint. Now this means that when I run the code, I'll just run it just like this, the code will run up to this point and no further, and then we will get information in the log that we can look at. Okay. So right now nothing is happening because I've stopped the code where things are living right here in the array. Now the breakpoint itself, I can get rid of this by right clicking and deleting it, or just dragging it out. Now what I'd like to see is the order of the colors, what color, what name of what color fits into this little variable right here. So I can add a breakpoint right here on line 16, and then run it again, and let's see what happens. Okay. So I'm getting to a point here and I can look in the log and I can see, oh, now I have this information here says colors, seven values, and here are the seven values. These are the positions of the array, but that doesn't tell me what is in this right here. So it looks like I've gotten to a point where I've stopped the code before telling me any other information, which makes sense because I've stopped on line 16, but line 17 is where I'm really putting the stuff in. So let's just change that. Now you can actually add this breakpoint, I mean, not the breakpoint, but you can move the thread up and down to do some experimentation. But I really want to show you the breakpoints itself. So I'll come over here to line 17, let's try this one more time. So now I see something different. I see right here, it says color string red. I see nothing on the screen because I've broken the logic of it, but I do see color right here. So if I take my breakpoint now and change it to 18, now I can see everything appearing on the screen because the loop itself, the for each lives right here. Go back upward one and stop this. Now we know where we came from. We saw color string red. Now I can step through my logic here with these little icons. So step into step out. This is really good if you're inside of a function. You want to see what's going on. So I can step over it and I can keep running. So if I just refine it, but I can see, oh, well, here's orange and now it's yellow and green. So you can see it's stepping through each item in the array, blue, indigo, violet, and that is how a breakpoint works. So breakpoints let you stop the flow of logic so you can test out your application. They're very handy within, let's say, a function when you want to step into the function and see what's going on, but it's a very good way of analyzing your code and seeing what's in there to help you know one last thing I do want to show you. So if I come back up here, let me just remove this. Run this again. Down here, you'll see at the very bottom, I'll make this a little bigger. This little thing in parentheses says LLDB. Now LLDB is a debugging tool and this is really part of the LLVM project. What I can do down here is actually put in a command so I can type p for print and then colors because I can look into here to see what's going on inside of here. I meant to say color, not colors, but here we go. So now I can see the value right here, string. This is the shortcut that if you remember inside of Swift and then the value of red. And if I keep moving through it one more time, get the value of orange, which is the next in the array, which is what we're seeing over here. Thanks for watching. Stay tuned for the next lesson where I'll show you how to debug with Capture View hierarchy.

Learn on the Go

Take your learning anywhere — the KnowledgeCity mobile app lets you watch lessons on the go.