Make Swift Playgrounds Apps: Learn About Swift Types

We’ve used integers and strings so far in our code for values. We use each differently, and their use we call a type. Numbers like integers are a type, as are strings as text. Swift is a strongly typed language. To best use Swift, you need to understand the basic types. 

We’re going to use the other swift playground for this. When I want to look at values carefully, the original playground is much better for doing so. So exit out of the app to the browser and tap the button with a plus in a rectangle to load a playground. You won’t be using the sidebar, so you can close that. 

I’ll type in two constants, similar to the ones We’ve used already.  

let size = 18 
let pizzaName = "Margherita"

Press the Run My Code button. You’ll see two new small buttons. Tapping either shows the value of the constant. 

Size is an integer, a number with no fractional value. Integers are very easy for CPUs to work with, so they are the fastest kind of number.  

PizzaName is a string. Strings are made of character combinations represented by sets of numbers. 

You can add strings to strings. You can add Integers to integers. But you can’t add an integer to a string like this

let pizzaName = "Margherita" + size

You get an error because they are different types and don’t mix. 

So far, we’ve used implicit typing to set the types. That means Swift guesses the type by the probable type you assign to the constant. The compiler can get it wrong, so it is a good idea to explicitly set the type with a type declaration. Change the code to this. 

let size:Int = 18
let pizzaName:String = "Margherita"

Int tells the compiler that size is an integer, and string tells the compiler pizzaName is a string. This tells programmers and the swift compiler what you are doing, thus making your code faster and easier to understand. 

There are a lot of different types and variations of types. Int has over a dozen variations alone. However, there are a few types you use more than others. Besides String and Int, you’ll use Double, Float, Bool, and CGFloat most often. 

Double and Float are Floating point numbers, that is, numbers with a decimal point. Float is smaller and less accurate, Double bigger and more accurate. In most code, you’ll want to use Doubles over Floats, but there are cases for an efficient number that use Float. Swift assumes you want a Double. 

Add the following code.  

let pi = 3.141
let area:Double = pi

pi is implicitly typed, and area is explicitly typed to Double. We get no errors because they are. However, change area to  

let area:Double = pi * size * size

And you get an error because the size is an integer. 

Change size to a Float, and it still is an error

let size:Float = 18

You have to make it a DOuble to work. 

let size:Double = 18

There’s another floating-point type which is a little weird, but you’ll see it often called CGFloat. It is the floating-point number used for graphics and layout. Depending on the device, it changes type between Float and Double. However, if you are using Swift playgrounds 4, CGFloat will act like a Double, so it won’t complain about the type change.  

let area:CGFloat = size * size * pi

However, it is not a native swift type, so we still get an error. It is part of a framework called Core graphics, which is included in the swiftUI Framework. If you add at the top of your code 

import SwiftUI

Swift learns what CGFloat is, and the error disappears

The last type you frequently see is Bool. Bool is a true or false type used for testing if something has a similarity or difference from something else. Add this to make a bool to indicate a big pizza.  

let isBigPizza:Bool = area >= 1000

Run the code. You get a true value since the area is more than 1000. Change the value to 8 and it is false. 

Those are the basic types you’ll find in Swift. There are a lot more, and as we’ll see later, you can make your own. In Swift, almost everything is a type. In our next lesson, we’ll move from constants to variables, so be sure to hit the rainbow pizza to subscribe, so you don’t miss any of our exciting lessons.  

The Whole Code

import SwiftUI
let size:Double = 8
let pizzaName:String = "Margherita"
let pi = 3.141
let area:CGFloat = pi * size * size 
let isBigPizza:Bool = area >= 1000

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: