Join Todd Perkins for an in-depth discussion in this video Challenge: Build a clicker app, part of Programming for Non-Programmers: iOS 12 and Swift 5.
(electronic music) - [Instructor] As we wrap up this chapter, I have a challenge for you. To create an app using conditional statements. Before we get into the challenge, I want to review what conditional statements are. They are if/then statements. In the if part of it, a condition is evaluated as true or false. The then part of the if/then statement is a block of code that is executed depending on the result. If the statement is evaluated as true, the if block runs. And you can have an else block that runs if that statement is evaluated as false. We've also talked about optionals and guard statements. We're going to see how those apply later on when we create our calculator. But for this challenge I'm going to leave those out of it. When you create a conditional statement you use the if keyword, and you can optionally wrap the condition in parentheses. Then you contain the then code inside of curly braces. That runs if the condition is true. If the condition is false, you can optionally run an else statement following the if statement. Remember, an else statement can only exist if there's an if statement before it. Let's look at some code that shows an if statement, so this code checks to see if some variable called score is greater than 50,000. If so, then the code runs if the condition is true. If not, we can have code that runs if the condition is false. We have some conditional comparison operators. And I want to point out that these are not the only operators. These are just the operators that are relevant to building the app in this course. So we have greater than, less than, equal to, which is two equal signs, and then the vertical pipes, which you can get to with Shift + \, and that is for multiple conditions and it means or. So if one or the other condition is true, then the whole statement is evaluated as true. So now let's get to the challenge. I want you to create an app that changes a label after 10 button clicks. If you want to take the challenge in hardcore mode, you can stop this movie now and get started on the challenge. For those of you who want some hints on how to approach the challenge, I'll give them now. Create a button, a label, the code connections with the button and the label, and an Int type variable to count the number of clicks. In your event handler for the button click, add one to your Int variable each time. Then you can have an if statement that checks to see how many clicks there were, and if there were more than 10 clicks, then you can change the label. This challenge should take you about 10 to 15 minutes to complete.
- Installing Xcode
- Creating an Xcode project
- How variables work
- Connecting visual objects to variables
- Creating a method in Swift
- Connecting a button to a method
- Using conditional statements
- Setting up the user interface (UI)
- Building a complete iOS app