Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

This Swift syntax quick reference covers the core language patterns students need when writing readable programs in Xcode or Swift Playgrounds. It helps grades 11 and 12 students remember how to declare values, choose types, write conditions, build loops, and organize code into reusable parts. A cheat sheet is useful because Swift has strict syntax, and small details like let versus var or optional unwrapping can change how a program behaves.

Key Facts

  • Use let name = value for a constant and var name = value for a variable that can change.
  • Common Swift types include Int for whole numbers, Double for decimals, String for text, Bool for true or false, and Character for one symbol.
  • Use if condition { code } else { code } to choose between branches, and conditions must evaluate to a Bool.
  • Use for item in collection { code } to loop through arrays, ranges, strings, or other sequences.
  • Use while condition { code } when the number of loop repetitions is not known before the loop starts.
  • Declare a function with func name(parameter: Type) -> ReturnType { return value }.
  • An optional type is written as Type?, such as String?, and it can store either a value or nil.
  • Use guard let value = optional else { return } or if let value = optional { code } to safely unwrap an optional.

Vocabulary

Constant
A named value declared with let that cannot be changed after it is assigned.
Variable
A named value declared with var that can be updated while the program runs.
Optional
A type that can hold either a normal value or nil to represent no value.
Function
A reusable block of code that can accept parameters, perform actions, and return a result.
Array
An ordered collection of values of the same type, accessed by zero-based index.
Struct
A custom value type that groups properties and methods into a reusable model.

Common Mistakes to Avoid

  • Using let for a value that needs to change is wrong because Swift constants cannot be reassigned after initialization.
  • Forgetting that array indexes start at 0 is wrong because the first element of items is items[0], not items[1].
  • Force unwrapping an optional with ! without checking it is wrong because the program will crash if the optional contains nil.
  • Writing a condition that is not a Bool is wrong because Swift does not treat numbers or strings as automatic true or false values.
  • Leaving out argument labels in function calls is wrong when the function definition requires labels, because Swift uses labels as part of the call syntax.

Practice Questions

  1. 1 What is the output of this code: var score = 8; score += 5; print(score)?
  2. 2 What is the value of nums[2] after this code runs: let nums = [4, 7, 10, 13]?
  3. 3 Write a Swift function named square that takes an Int parameter named n and returns n multiplied by itself.
  4. 4 Explain why if let name = username { print(name) } is safer than print(username!) when username is a String?.

Understanding Swift Syntax Quick Reference

Swift is designed to catch many mistakes before an app runs. Its type system is a major reason. When Swift knows a value is an integer, text value, or true false value, it can reject operations that do not make sense.

Type inference often works out the type from the first assigned value. Clear type annotations become useful when the value is not obvious, when a collection starts empty, or when you want readers to understand a data model quickly.

A compiler error is not just a blockage. It often points to a mismatch between the kind of data a program expects and the kind it received.

Choosing between a struct and a class affects how data moves through a program. Structs are value types. Assigning a struct to a new variable usually makes an independent copy.

Changing the copy does not change the original. This makes structs a good fit for small models such as a score, a location, or a game card. Classes are reference types.

Two variables can refer to one shared object, so a change through one reference can be visible through the other. This is useful for objects with a shared identity, though it can make bugs harder to trace. Students should pay close attention to whether a change is meant to affect one value or every reference to an object.

Collections help programs handle groups of information. An array keeps items in an order and is useful for a playlist or a list of quiz scores. A dictionary links a key to a value, which suits data such as a student name paired with a grade.

A set stores unique items, making it useful for tracking which tags or usernames have appeared. Before choosing a collection, think about the job. If position matters, an array is often appropriate.

If fast lookup by a label matters, a dictionary may fit better. If duplicates would be a problem, a set can prevent them. Index errors are common with arrays, especially when code assumes an item exists at a position that is outside the valid range.

Optionals model missing information honestly. Real programs regularly receive incomplete data from text fields, files, network requests, or searches. An optional forces code to deal with the possibility that no usable value exists.

Safe unwrapping creates a path for the present value while keeping the missing case separate. Guard statements are especially helpful near the start of a function because they remove invalid cases early and leave the main work less indented.

Functions become easier to test when each one has one clear purpose, sensible parameter names, and a predictable result. When debugging, read errors carefully, print small pieces of state when needed, and test boundary cases such as an empty array, zero, negative input, or absent text.