Swift

Swift is a powerful and intuitive open-source programming language developed by Apple Inc. for building applications across Apple’s ecosystem, including iOS, macOS, watchOS, and tvOS. It is also increasingly used for server-side development and other platforms due to its performance and modern features.

https://www.swift.org

High Level Language Review

Swift in 100 Seconds
How to Code in Swift
Swift for Beginners
Build your next website in Swift

Xcode vs. Swift Playgrounds

Swift Basics

Swift Basics #1

Section Overview

  • Installing Xcode: 2:49
  • Creating an Xcode playground: 4:03
  • Print to Console: 7:44
  • Comments: 9:19
  • Naming Conventions: 12:43

Swift Basic Types

How to use basic Types in Swift (Bool, String, Int)

Section Overview

  • Strings: 4:03
  • Boolean: 6:57
  • Type Safety!: 8:34
  • Dates: 11:02
  • Numbers: 12:18

Swift Variables and Constants

Section Overview

  • Var vs. Let: 2:02
  • Should I use var or let?: 3:52
  • More examples: 4:37
  • If Statements: 6:46

Class vs. Struct

Swift – Class vs. Struct Explained

Both used to create objects.

  • Class = reference type
  • Struct = value type
  • Class = shared and mutable
  • Struct = copies

Class has inheritance which means you can create a subclass that inherits properties from the initial class.

The variables of a class must be initialized.

Swift Code Examples

Hello World:

print("Hello, world!")

Swift Basics ( Reference: https://www.youtube.com/@SwiftfulThinking )

import Foundation

var greeting = "Hello, playground"

print(greeting)

// Single line comment

/*
 Line 1
 Line 2
 Line 3
 Multiple line comment
 */

/*
Naming Conventions
Variable and Constant names in camel case

 Correct
 Camel Case Example: theFirstDayOfTheWeek
 
 Not Correct
 Pascal Case Example: TheFirstDayOfTheWeek
 Snake Case Example: the_first_day_of_the_week
 */

Swift Basic Types( Reference: https://www.youtube.com/@SwiftfulThinking )

import UIKit

// "String"
let exampleOne = "Hello, playground"

// Boolean true or false
let exampleTwo = true

// Date
let exampleThree = Date()

// Numbers Int, Double, CGFloat

// Int
let exampleFour: Int = 42

// Double
let exampleFive: Double = 3.14

// CGFloat - often used is UI xy design
let exampleSix: CGFloat = 100.0

Variables and Constants:

var greeting = "Hello" // Declares a variable
let name = "Alice"     // Declares a constant

greeting = "Hi" // Variables can be changed
// name = "Bob" 
// Constants cannot be changed (this would cause an error)

Functions:

func greet(person: String) {    
    print("Hello, \(person)!")
}

greet(person: "Bob") // Calling the function

Control Flow (If/Else):

let temperature = 25

if temperature > 30 {
    print("It's hot!")
} else if temperature < 10 {
    print("It's cold!")} else {
    print("The weather is pleasant.")
}

Arrays:

var fruits = ["Apple", "Banana", "Cherry"]
print(fruits[0]) // Accessing elements by index 
//(output: Apple)

fruits.append("Date") // Adding an element
print(fruits) 
// output: ["Apple", "Banana", "Cherry", "Date"]

Loops (For-in):

for fruit in fruits {
    print("I like \(fruit).")
}

Structures:

struct Book {
    var title: String
    var author: String
    var year: Int
}

let myBook = Book(title: "The Great Gatsby", author: "F. Scott Fitzgerald", year: 1925)

print(myBook.title) // output: The Great Gatsby

Class:

class = myCar {
    var = year: Int
    var = color: String

    init (year: Int, color: String){
        self.year = year
        self.color = color
     }
}

Operators