Lesson 4: How to Write App Logic With Swift 4 Code

Welcome to Lesson 4 of iPhone Apps 101!

If code seems mysterious or stressful, don't worry. In this lesson I'll walk you through the process to make the app work.

You'll learn best if you type the code as we go. Pause the video and follow along if I go too fast.

Everything we do today is case sensitive and spelling has to match. If you get stuck you can use the resources below to look at the final solution.

Download the Resources and Xcode Project for Lesson 4

Included is the beginning and final version of the app. You can always make a copy and start over from the beginning.

Apps Need Brains

An app without any logic is just a set of buttons, labels, and images.

To make your app "feel" smart you just need to give it rules for what it needs to do.

Code is very much like a recipe, and it's a recipe that we can repeat as many times as we want.

When you connected the UI to the code in the last lesson, you now have a function (a chunk of code) that can be reused and called later on.

Code is modular, like Legos. You create an initial design using an instruction manual (the recipe) and then you can make copies that look exactly the same. The big distinction is that with code, you are designing interactions and behavior in addition to look and feel.

Numbers, Strings, and Currency

You will get hands on experience with numbers, String's and currency in this lesson.

Numbers are values that can be added or multiplied. In code these are different than a String. A String is like a sentence, it doesn't really make sense to add it.

When you work with user input, you get String's, and you'll need to convert to numbers (Double, Int, Float, etc.) to perform calculations.

You'll learn how to convert a number back into a formatted String, so that a floating-point number like 43.0 can be printed as "$43.00" to the user interface.

Download the Resources and Xcode Project for Lesson 4

Challenges

Try doing the entire lesson again, but this time do it without the video. Start over with the "4.0 - Tip Calculator UI - Begin" Xcode project, and do it again.

Practicing will help you get more muscle memory and familiarity with writing code (even if you don't fully understand it!).

Take the app a step further and customize it:

  • Bonus 1: Your beta tester has a feature request. The current design doesn't display the final bill (since there were 3 tips presented). How will you solve it? Add the final bill using one of the following: your tip UI sketch, 3 final bills under each tip, a popup UILabel when you tap each tip percentage (Feel free to change the UI drastically if that helps using your previous sketch).

  • Bonus 2: Double isn't the best number to use for currency, since it is not exact. Change all the Double variables to NSDecimalNumber: Use the Quick Help for NSDecimalNumber or try searching for how to use it.

  • Bonus 3: Add a missing feature for bill splitting using another UITextField for user input and a new per-person UILabel amount.

Summary

You just finished your first functional iPhone app!

Great work!

Today you learned how to work with text input from the user.

  • How to parse currency text with a NumberFormatter
  • How to format numbers with a NumberFormatter
  • How to perform a calculation between two numbers in Swift
  • How to create functions (chunks of code)
  • How to fix common your first code bugs

Now you're ready for the next step.

Send me an email: Paul@SuperEasyApps.com if you had any problems.

-Paul

P.S. Check your email tomorrow for a bonus lesson!