📱

CS50 iOS Track 1: Introduction to Swift and iOS Development with Xcode

Jun 14, 2024

Introduction to Swift and iOS Development with Xcode

Introduction

  • Writing iOS apps with Xcode (Apple's IDE)
  • Programming language: Swift (recommended over Objective-C)

Swift Programming Basics

Variables

  • Immutable vs Mutable
    • Immutable: Declared with let, cannot change value once set
    • Mutable: Declared with var, value can be changed
  • Syntax: let/var variableName: Type = value

Data Types

  • Built-in Types: Integers, Doubles, Floats, Strings (all capitalized)
  • Additional Types: Arrays, Dictionaries

Variable Declaration

  • Mutable: var count = 50
  • Immutable: let title: String = "..."
  • Type inference capability

Conditions and Printing

  • Conditionals: No need for parentheses around conditions
    • Example: if condition { ... } else { ... }
  • print function: Simplified from printf (no %d, %f necessary)

Arrays

  • Declaration: let values: [Int] = [1, 2, 3]
  • Iteration: for value in values { ... }

Dictionaries

  • Declaration: let airports: [String: String] = ["SFO": "San Francisco", "BOS": "Boston"]
  • Iteration: for (code, name) in airports { ... }
  • String interpolation: print("\(code), \(name)")

Functions in Swift

  • Function syntax: func functionName(parameters) -> ReturnType { ... }
  • Example: func sayHello(name: String) -> String { ... }
  • Note: No semicolons required

Calling Functions

  • Syntax: functionName(parameterName: value)

Structs and Classes

Structs

  • Like containers for data
  • Example: struct Course { let name: String let instructor: String }

Classes

  • More powerful, can contain methods
  • Declaring a class: class ClassName { ... }
  • Example: class Person { var name: String init(name: String) { ... } func sayHello() { ... } }
  • Instantiation: let person = Person(name: "Tommy")

Methods

  • Defined inside classes
  • Example: func sayHello() { ... }

Inheritance

  • Subclassing: class SubclassName: SuperclassName { ... }
  • Overriding methods with override: override func methodName() { ... }

Advanced Concepts

Protocols (Interfaces)

  • Declaring protocols: protocol ProtocolName { func methodName() }
  • Using protocols in classes: class ClassName: ProtocolName { ... }

Optionals

  • Optional Type: var name: String?
  • Handling optionals: if let unwrapped = optional { ... } else { ... }
  • Implicit Unwrapping: Dangerous for app crashes
  • Guard Clauses: guard let unwrapped = optional else { ... }

Writing Your First Swift Code

Xcode Setup

  • Opening Xcode and creating a new project
  • Project Details:
    • Product name
    • Organization name and identifier
    • Language: Swift

Writing Code in Xcode

  • Example: Sorting Hat program
  • Declaring a class: class Track { let name: String let instructor: String init(name: String, instructor: String) { ... } }
  • Creating instances: let tracks = [Track(name: "Mobile", instructor: "Tommy"), ...]
  • Iterating and Assigning: for student in students { let track = tracks[Int.random(in: 0..<tracks.count)] assignments[student] = track }
  • Printing assignments: for (student, track) in assignments { print("\(student) got \(track.name) with \(track.instructor)") }