Make App Pie

Training for Developers and Artists

Make Playgrounds Apps: Constants and Calculations


We’ve got an application started, but it is very hard to change to do anything with.  If I wanted something other than guitars, such as a lightbulb or tv, I’d have to change the entire program. 

Swift playgrounds and its bigger brother on the Mac, Xcode,  have special programs called compilers and interpreters. These take our typed words and make them readable by the chips inside your iPad or Mac. There languages use interpreters too, such as Javascript in your web browser. 

Swift and most programming languages have a way for developers to tell the  that there is a name for a value. We call this a constant. IN swift we assign it  like this above 

let thing = "guitars`

The let is a keyword, a special word that tells Swift that what follows is an assignment of a constant. The word thing is an identifier, a name for a value, which I put after an equals sign.  It can be any word, but we usually leave the first letter lowercase to tell us it is an identifier.

Once you define an indetifier, you can use it. Fro example I can change the Image to this:

Image(systemName: thing)

And we see out guitars

 I can’t put a constant like thing directly into a string, I can however tell the compiler this is a value with interpolation characters of backslash and parentheses like this; 

Text("Hello, \(thing)")

I can change the value of thing, and everything that uses it changes. For example I’ll make it 

let thing = "tv"

And I see tv’s. Or a lightbulb 

let thing = "lightbulb"

And we get lightbulbs.

I’m not limited to strings. I’ll change thing to 5

let thing = 5

And I get an error. If I. Look at the error, it is the compiler whining that Image doesn’t understand integer numbers, only strings.  I can fix that with interpolation, 

`Image(systemName: “\(thing)”)`

And now it works, but is blank. The text shows correctly though. There is no sf symbol named 5 so it is blank. But there is the number 0 through 50   with .square or .circle after it.  So I can change the string to 

Image(systemName: "\(thing).square")

And it works. 

Once you start working with numbers, you can also start working with math. There are four operators you can use  with numbers. The first is addition

let thing = 5 + 3

THIng now equals 8, and we see that in the preview. Subtraction is with a minus sign

let thing = 5 - 3

Which assigns 2, and we see that in the preview too. Division is a forward slash

let thing = 5 / 3

And this gets us a result you might not expect of 1. The numbers we are using are Integers, numbers without fractions, so the result is an integer value.  But there some left over from one and the remainder  operators can show us that

let thing = 5 % 3

Which is two.  IF you really want the fractional value, use a decimal point after the value. 

let thing = 5.0 / 3.0

You’ll see the value but notice we have no symbol. There are only SF symbols for the integer 0 through 50. 

You are not limited to only one operator I’ll change this to 

let thing = 5 * 3 – 1

I get 14. Multiplication and division are calculated before addition and subtraction. 

When a constant is a number you can do math to it, since it represents a number. For example I can do this

Image(systemName: "\(thing + thing ).circle")

I’ve added thing to itself.  I can also make another constant from thing, but you have tone careful. I’ll make a thing2 under thing

let thing2 = thing + 2

And I get an error message.  Cut and paste this Above Vatack, and the error message gets away.  Constant declarations must be in specific places. I’ll discuss why in a later video after we have a few more concepts down. 

Meanwhile we have a new error message which says  thing2 is lonely because no one is using it. I’ll assign it to my text. 

Text("Hello, \(thing2)")

And we get  a response on the preview. 

You’ve covered some more basics of assignment and some of the math functions you can use with integers.  Next time, I’ll show you some more sophisticated  stuff you can do with numbers and strings called functions. 

The Whole Code

if you want to look at everything we’ve done or copy and paste into playgrounds, here’s all the code we’ve used so far.

import SwiftUI

struct ContentView: View {
    let thing = 5 + 3 * 2
    var body: some View {
        let thing2 = thing + 5
        VStack {
            Text("Hello, Pizza!")
            Image(systemName: "\(thing - 1).square")
            Text("Hello, \(thing2)!")

Leave a Reply

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

You are commenting using your 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: