Swift is a modern, open source programming language.
Installation
You can install Swift by downloading Xcode from the Mac App Store.
To verify the installation, run the following command in the terminal:
1
swift --version
You can create swift file with .swift extension. To run a Swift file, use the following command:
1
swift <filename>.swift
Hello World
1
print("Hello, World!")
Data Types
Swift basic data types are Int, Double, String, Bool, Array, Dictionary, Set, Tuple, Optional, and more.
1 2 3 4 5 6 7 8 9
let a =10; let b =10.0; let d ="Hello, World!"; let e =true; let f = [1, 2, 3]; let g = ["a": 1, "b": 2]; let h =Set([1, 2, 3]); let i = (1, 2, 3); let j =Optional(1);
Array
Arrays are declared with []. Array is a collection of elements of the same type. Common operations include append, remove, insert, and count.
variables are declared with var, constants are declared with let. You can also declare variables with type annotation.
1 2 3 4
var a =10; let b =10.0;
let names: [String] = ["John", "Jane", "Jack"]
Enum
Enum is a collection of related constants.
1 2 3 4 5 6 7 8 9 10
enumDirection { case north case south case east case west }
let direction =Direction.north
print(direction)
If Statement
1 2 3 4 5 6 7
if a > b { print("a is greater than b") } elseif a < b { print("a is less than b") } else { print("a is equal to b") }
Switch Statement
1 2 3 4 5 6 7 8
switch a { case1: print("a is 1") case2: print("a is 2") default: print("a is not 1 or 2") }
Loops
1 2 3
for i in1...5 { print(i) }
1 2 3 4
while a >0 { print(a) a -=1 }
Function
A function is a block of code that performs a specific task. Functions can take parameters and return values.
1 2 3 4 5 6 7 8 9
let names: [String] = ["John", "Jane", "Jack"]
funcprintArray(arr: [String]) { for name in arr { print(name) } }
printArray(arr: names)
Function with return value
1 2 3 4 5 6 7
funcisPalindrome(s: String) -> Bool { let str = s.lowercased() let reversedStr =String(str.reversed()) return str == reversedStr }
print(isPalindrome(s: "Catac"))
Function with multiple parameters
1 2 3 4 5
funcadd(a: Int, b: Int) -> Int { return a + b }
print(add(a: 1, b: 2))
Function with multiple return values
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
funcgetMinAndMax(arr: [Int]) -> (Int, Int) { var min = arr[0] var max = arr[0] for num in arr { if num < min { min = num } if num > max { max = num } } return (min, max) }
overridefuncprintDescription() { print("This is a car") super.printDescription() } }
let car =Car() car.printDescription()
Protocol
Protocol is a blueprint of methods, properties, and other requirements that a class, struct, or enum must implement.
Example: protocol Vehicle with required properties and methods. numberOfWheels is a read-only property, numberOfSeats is a read-write property, and printDescription is a method.
funcprintDescription() { print("This is a car") } }
var car: Vehicle=Car(numberOfWheels: 4, numberOfSeats: 5) car.printDescription() // car.numberOfWheels = 6 // error: number of wheels is a get-only property car.numberOfSeats =7 print(car.numberOfWheels) print(car.numberOfSeats)
Extension
Extension is a way to add new functionality to a class, struct, or enum.