At the level of hardware and electricity, a computer only understands high voltage and low voltage.

High voltage, or on, corresponds to a 1.

Low voltage, or off, corresponds to a 0.

Computers use the abstraction of binary numbers to represent numeric values.

How these 1’s and 0’s are stored at a physical level varies depending on the storage medium.

For example, in a compact disc pits and lands are used to register 1’s or 0’s.

Bits

A bit is a single piece of information – literally a 1 or a 0.

As we just learned, we can use the abstraction of the base 2 number system to represent numbers in base 10.

Four bits can represent a minimum of zero:

Value expressed as a power
Value expressed in standard form
 

So,

Four bits can represent a maximum of fifteen:

Value expressed as a power
Value expressed in standard form
 

So,

Variables, Constants, Data Types

In your playground, the default code provided is:

NOTE

Cocoa is a macOS framework provides basic functionality for apps written to run on the Mac.

Xcode will attempt to run code in your playground immediately, as you type it – this can often be bothersome – so, long press on the ▶ button in the bottom left corner and then choose Manually Run:

Then actually run your playground by clicking the â–¶ button, or by pressing Command-Shift-Return on your keyboard:

After a few seconds – note that it takes a bit longer the first time you run a playground – you will see results appear in the sidebar:

This code creates a variable named greeting.

A variable can hold a single value at a time.

The variable named greeting holds the value Hello, playground.

Now hold down the Option key on your keyboard.

Position your cursor over the greeting text.

You will see a question mark appear.

Click the trackpad or your mouse button.

The Swift compiler that is part of Xcode will guess that the data type of the greeting variable is a String:

NOTE

When Swift’s compiler “guesses” a data type, we call that type inference.

A string is a programming term for a data type that holds text.

In Swift, strings are always surrounded by quotes – they must begin and end with a ".

TIP

Later this year, you will learn what the text @MainActor means.

Add the following code to your playground to express your age – Mr. Gordon’s age will be a little greater than yours:

let age = 48

Run the program. This creates a constant named age.

Option-click the age constant.

Swift reports the data type as an integer, or Int:

In Swift, on modern computers, an Int is stored using sixty-four bits!

One bit is used to store the sign – that is, whether the number is positive or negative.

That leaves sixty-three bits to represent a range of values from  -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.

To be clear, an Int in Swift is stored this using the very same system of abstraction for binary numbers that we just explored. The only difference is that one bit is used to store the sign to mark a value as being positive or negative.

Try celebrating Mr. Gordon’s birthday a little early – it’s actually on February 21 – by adding one to the age constant:

let age = 48
age += 1

Does this work?

It does not, because we cannot change a constant value:

You can also comment out code that tries to change the age like this:

let age = 48
//age += 1

Now the code is still there for us to read, but the computer will ignore it when you run the playground.

Finally, add the following code to your playground to represent an imaginary bank balance:

var balance = 1_000_000.49

Run your code and Option-click it to find it’s data type:

TIP

At this point, if both people in your group have a computer with Xcode installed, use AirDrop to send the playground to your partner.

Complete the rest of this activity on your own computer, but feel free to check answers or discuss ideas with your partner.

Take note

Go to your portfolio on Notion.

Make a brief note for yourself, based on what you have experimented with in this exercise.

  1. What is a variable?
  2. What is a constant?
  3. What are the three main data types that we will use, for now, in programs that we write?
  4. For each data type, write out two examples of information that could be stored

Easter egg, of sorts

Mr. Gordon has mixed feelings about user interfaces that reveal information only when the mouse cursor is in a certain position.

Using this clue, can you find another way to determine the data type of a variable or constant created in an Xcode playground?

Exercises

TIP

Complete these exercises in the same playground file that you were working with above.

When you are done, share your code in your portfolio using a screenshot or by typing /code to create a code block – then you can copy and paste the code directly in to your portfolio post.

  1. Create a value that holds the name of a course you’re taking. Decide whether it should be a variable or constant, add a short comment explaining your choice, then Option-click the name to confirm the inferred type.

  2. Create a value for the outside temperature right now. Decide variable or constant, add a brief comment, then Option-click to check the inferred type.

  3. Create a value that stores a short status message you might post to classmates in a chat app. Should it be a variable or a constant? Option-click to see the data type Swift infers.

  4. Create a value for the number of steps you have taken so far today (make an estimate). Add a one-line comment about your data type choice. Option-click to verify the data type that Swift inferred.

  5. Create a value for the price of a snack at the school store. Explain your variable or constant choice, then Option-click to confirm the type.

  6. Create a value for a room label you might use in a school map (letters and numbers together). What data type makes sense? Make a comment to explain why you chose variable or constant, then Option-click to check the type.

  7. Create a value for the count of seats in a classroom you’ve been in. Add a short comment justifying variable or constant, then Option-click to confirm the type.

  8. Create a value for your computer’s screen size in inches. Comment on your choice and Option-click to verify what Swift inferred.

  9. Create a value for our school’s name. Add a brief comment about whether it should, or could, change during the program, then Option-click to see the inferred type.

  10. Create a value for the number of books you hope to read before the year is over. Add a one-line comment about variable or constant, then Option-click to check the type.

  11. Create a value for a distance you walk between your house and the Academic Block here on campus. Explain your variable or constant choice and Option-click to verify the type.

  12. Create a value for a jersey number you like. Add a comment about your choice and Option-click to confirm what Swift inferred.

  13. Create a value for a battery level one of your devices might report. Comment on whether it should be a variable or constant, then Option-click to check the type.

  14. Pick a large number from any real-world context (e.g., a population or follower count). Write it using digit underscores for readability. Add a short comment explaining why the underscores help, then Option-click to confirm the type.

  15. Create a value for a classroom item’s label (something you could put on a storage bin). Add a brief comment about your choice and Option-click to verify the type.

  16. Review all of your values. Mark three that make sense as constants in real life and add a short comment to each explaining why. Mark three that make sense as variables in real life and add a short comment to each explaining when or why they might change.

Partial solutions

Here are a set of solutions to the even-numbered exercises above. These are just one possible set of answers. Please review, comparing these to your own work. Ask questions of Mr. Gordon through your portfolio on Notion using @Russell Gordon as needed. You will see questions like these on Monday’s mini-test. Note that the mini-test on Monday will be written on paper – no computers or other reference material permitted.