Swift Basics — Week 3

Data Types

Ryan Cortez
5 min readJul 17, 2016

📅 Last week

We talked about Constants and Variables, how to store information, and how to pull out that information when needed. I also left you with a homework assignment, let’s review that first.

📚 Homework Review

Normal Mode

1. In your Playground file, make a Constant using whatever name you would like

2. Make a Variable using whatever name you would like

3. Change the value of the Variable to some other value

One possible solution:

Below I’ve made a constant named “whateverNameYouWant” with the value of six. I’ve also made a variable named “anotherName” with the value of two, then I changed the of anotherName t0 three.

let whateverNameYouWant = 6var anotherName = 2
anotherName = 3

Hard Mode — Tip Calculator

Write some code that calculates the tip you would tip a waiter at a restaurant.

One possible solution:

If you take the percentage you want to tip and multipy that by the restaurant bill and then divide that by 100. You’ll get the amount that you want to tip. Then it’s as simple as adding the amount you want to tip with the restaurant bill.

let restaurantBill = 50
let tipPercentage = 20
let tipInDollars = (tipPercentage * restaurantBill) / 100let restaurantWithTip = restaurantBill + tipInDollars

A shorter solution:

Most of the time when you’re writing code you’ll want to make the code as clear and concise as possible. We can remove one line by replacing tipInDollars.

let restaurantBill = 50
let tipPercentage = 20
let restaurantWithTip = restaurantBill + (tipPercentage * restaurantBill) / 100

Constants and variables are an important foundational concept and you’ll use them in every project you make. This week is another important concept, Data Types. You’ll need to practice using these before we move to other topics. Feel free to explore the Swift playground.

💭 Data Types: (The Concept, without code)

Data type: — The type of information that is stored in a variable

So far, the variables you’ve been making have been just whole numbers. Without decimals, and without any words or letters. Wouldn’t it be nice to be able to store any kind of information? Here’s a list of the Data Types that we will cover and examples what would be stored in each type.

What do Data Types do for me?

They make it possible to store any kind of information in a variable.

💻 Data Types: (Code Examples)

Let’s take a look at what these look like in code:

I have three constants. The first is an integer, the second is a float, and the third is a string. One of the nice features of Swift is that it makes a guess on whatever type you’re using and you don’t have to explicitly write what type you’re going to use, in other languages you’re forced to do that. This is how you would declare each data type explicitly in Swift.

Example: — Explicitly Declaring Data Types

Nothing too crazy, just a “:” after the variable/constant name, and then add the type.

Ok, I’ve been hiding something from you 😞. The float that we’ve been using is not actually a float. It’s a different type called a Double. Now that you know what a float is though. I can share the difference in an example.

Float vs. Double

Let’s say you’re a scientist and you wanted your math to be very precise. You would use a double. A double, has almost double the amount of decimals places, compared to a float. Let’s look at what that means in code.

I’ve got this really precise number that I want to save. 1.111111111111111111111111111111111

In the first constant named “decimalNumber”. I saved the number as a float and I only get six decimal places. (You can see what’s saved in the results panel on the right.)

In the second constant named “decimalNumber2”, I saved the exact same number as a double and got fifteen decimal places!

This is much more precise, but it uses more memory in the computer you’re programming for, but that’s not something you’ll need to worry about for a long time. Computers are relatively large in terms of memory. I just wanted to give you the definitions of Float and Double so that you can follow other guides in the future.

By default, Swift saves your decimals as doubles. As demonstrated here:

You can see the fifteen decimal places on the right.

Example: — Converting Data Types (also known as “Casting”)

So let’s say I want to add an integer and a double. We should just get both of them added together right?

Wrong, Swift doesn’t like this. It wants you to only add or perform operations on things that are of the same time. The reason is, this makes your programs more stable. It prevents your App from crashing or closing out and going to home screen. It feels overly strict, but you’ll see that it’s a good thing in the long run.

So, how do we fix this? We need to convert the integer to a double. Just wrap it up like this, this is called casting your constant.

Double(wholeNumber)

Now the error that we had is gone, and we recieve the result we expected.

Conclusion

Data Types will be an essential part to every project you start. It’s important to experiment with them. Be sure to do the homework this week!

📒 Homework

Create all of the data types you know so far

  1. Create an Integer
  2. Create a Float
  3. Create a Double
  4. Create a String

If you have any feedback for me tweet me at @ryancortez. Catch you next week! 😎

📅 Next Week: — Collection Types

--

--

Ryan Cortez

A mediocre Software Engineer based in San Jose, California