CS193p Reading Assignment 1
The Basics
1.) Constants and Variables
Associate a name with a value of a particular type. For instance, maximumNumberOfLoginAttempts or welcomeMessage could hold the values 10 and “Hello!”.
The major difference between constants and variables is that variables may hold values that change over time, whereas constants, once set, must remain...well...constant.
Declaration:
let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0
Declaration of multiple variables or constants can be made on a single line like this:
var x = 0.0, y = 0.0, z = 0.0
Type Annotations help make it clear what kind of values a particular variable or constant can store. To provide a type annotation, separate the name of the variable or constant and its type by a colon, like this:
var welcomeMessage: String
can then be set
welcomeMessage = “Hello”
Type Annotations are typically rare as Swift is a strongly typed language and supports type inference, which is a fancy way of saying that the language understands what type a variable or constant is supposed to be as soon as it is set. Type Annotations help when initialization does not happen right off the bat, like the above example.
Constants and variables can hold almost any character, including Unicode. The following examples are all valid:
let ∞ = "forever”
let 1️⃣2️⃣3️⃣ = 123
Sometimes it’s helpful to print the values of various variables or constants throughout the development process. In Swift, printing those values to the debug area of Xcode is easy using the println() global function. println() prints a value followed by a line break to the console pane (debug area) at the bottom of the main Xcode window.
The println() function can print more complex logging message that include the current values of constants and variables. To do so, we use what is known in the Swift language as string interpolation. Interpolated strings act as placeholders within longer strings that will display dynamic values. To interpolate a string in Swift, wrap the name in parentheses and escape it with a backslash, like this:
var favoriteFood = “BBQ”
println(”My favorite food is \(favoriteFood).”)
this would print
My favorite food is BBQ.
2.) Comments
When the need to include non-executable text within your code arises, comments can be an extremely useful tool. Comments are left out when code is compiled and can be included in one of two ways. Comments are great ways to include things like notes or reminders to yourself.
Single-line comments begin with two forward-slashes, like this:
// this is a comment
Multiline comments are wrapped by forward-slash / asterisk combinations, like this:
/* this is also a comment,
but it is written over multiple
lines */
Comments in Swift differ from comments in C in that multiline comments can be nested within other multiline comments, like so:
/* this is the start of the first multiline comment
/* this is the second, nested multiline comment */
this is the end of the first multiline comment */
3.) Semicolons
Swift differs from many other languages in that semicolons are not required after each statement in your code. Although the compiler won’t mind if you do, it’s absolutely unnecessary for you include them. The only time semicolons become necessary is when you want to write multiple separate statements on a single line, like this:
let baaaah = “🐑”; println(baaaah)
4.) Integers
Whole numbers with no fractional components such as 8 or 15 or -150 are known as integers. Integers can either be signed (positive, zero, or negative) or unsigned (positive or zero).
Types of signed and unsigned integers provided by Swift and their respective type names:
8 bit (UInt8, Int8)
16 bit (UInt6, Int16)
32 bit (UInt32, Int32)
64 bit (UInt64, Int64)
Accessing the minimum and maximum values of each integer type is easy, just access the min and max class properties using dot notation, like this:
let minimumValue = UInt8.min
minimumValue is equal to 0
let maximumValue = UInt8.max
maximumValue is equal to 255
Usually you don’t need to be concerned with the specific size on an integer in your code. That’s why Swift provides an additional type, Int, which has the same size as the current platform’s native word size. So, on a 32-bit platform, Int is the same size as Int32. On a 64-bit platform, Int is the same size as Int64. Expectedly, Swift also offers an unsigned versions of Int as well - UInt. On a 32-bit platform, Uint is the same size as UInt32. On a 64-bit platform, Uint is the same size as UInt64.
It’s not common to specify an Int as unsigned (U) and Int is typically preferred, even when the values to be stored are known to be non-negative. This practice promotes code interoperability and keeps you as a programmer from having to convert between different number types. It’s also more closely aligned with Swift’s strong type inference system for integers.
5.) Floating-Point Numbers
Whereas integers represent whole values, floating-point numbers represent values with fractional components. For instance, 3.14159, 0.1, and -273.15 are all examples of floating-point numbers. These kinds of numbers can represent much larger ranges of values than integers. In Swift, you’re provided two types of floating-point numbers:
Double
64-bit floating point number. Typically used in cases where floating-point values need to be very, very large or incredibly precise.
Offers precision level of at least 15 decimal digits.
Float
32-bit floating point number. Use when floating-point values don’t necessarily need to include 64-bit precision.
Offers precision of at least 6 decimal digits.
6.) Type Safety and Type Inference
Due to Swift’s construction as an extremely type-safe language, you’ll need to be extra careful when assigning values to parts of your code. You won’t be completely on your own, though. As you’re working, the compiler will pick up any type mismatches and warn you about them so you can catch your mistakes early on. For instance, if you try to assign a String to a variable that’s already been typed as an Int, you’ll get an error message and get a chance to correct your mistake before your whole program crashes. As mentioned above in the section surrounding Type Annotations, you won’t actually need to specify what type of value a variable should be expecting if you provide a value at the time of initialization. These values, which appear directly in your source code, are known as literal values. For example, assigning the value 7 to a new constant called myFavoriteNumber will let the compiler know behind the scenes that the new constant is of type Int. This is known as type inference.
In the case of floating-point numbers, Swift always chooses Double (rather than Float) during type inference. For example, in the statement let pi = 3.14159, pi is inferred to be of type Double. If you were to combine an integer with a floating-point literal, Swift will automatically combine them into an inferred Double. For example, let anotherPi = 3 + 0.14159 will be inferred to be of type Double.










