Swift Language

Swift Language

Overview about the Swift Language

Details
  • On Xcode, go to File>New>Playground.
  • Choose iOS platform and the Blank template.
  • In this section, we can use the Playground to test out snippet of codes.
Constants, Variables, and Data Types
  • Constants and variables must be declared before they’re used. You declare constants with the let keyword and variables with the var keyword.
1
2
let maximumNumberOfLoginAttemps = 10
var currentLoginAttempt = 0
  • You can declare multiple constants or multiple variables on a single line, separated by commas:
1
var x = 0.0, y = 0.0, z = 0.0
Type annotations
  • You can provide a type annotation when you declare a constant or variable, to be clear about the kind of values the constant or variable can store.
  • Write a type annotation by placing a colon after the constant or variable name, followed by a space, followed by the name of the type to use.
1
var welcomeMessage: String
Printing constants and variables
  • Swift uses string interpolation to include the name of a constant or variable as a placeholder in a longer string, and to prompt Swift to replace it with the current value of that constant or variable.
  • Wrap the name in parentheses and escape it with a backslash before the opening parenthesis:
1
2
var friendlyWelcome = "Bonjour"
print("The current value of friendlyWelcome is \(friendlyWelcome)")
Semicolons
  • Unlike many other languages, Swift doesn’t require you to write a semicolon (;) after each statement in your code, although you can do so if you wish.
  • However, semicolons are required if you want to write multiple separate statements on a single line.
Most common types in Swift
  • Types:
    • Integer
    • Double
    • Boolean (true, false)
    • String
    • Swift
  • You may have noticed that you don’t have to specify the type of value when you declare a constant or variable. This is called type inference.
  • Swift uses type inference to make assumptions about the type based on the value assigned to the constant or variable.
1
let pi = 3.1415

or

1
let pi: Double = 3.1414
Create your own type
1
2
3
4
5
6
7
8
9
10
import UIKit

struct Car {
    var make: String
    var model: String
    var Year: Int
}

var c = Car(make: "BMW", model: "X3", year: 2022)
print (c.make)
Operators
  • Assignment:
1
let jedi = "Luke"
  • Basic arithmetic
1
2
3
4
var opponentScore = 3 * 8
print(opponentscore)
var myScore = 100 / 4
print(myScore)
  • Compound assignment
1
2
3
4
myScore += 3
myScore -= 5
myScore *= 2
myScore /=2
  • Numeric type conversion
1
2
3
4
let x = 3
let y = 0.1415
let pi = x + y /* not work */
let pi = Double(x) + y

Control Flow

Logical and comparison operators
  • ==
  • !=
  • >
  • >=
  • <
  • <=
  • &&
  • ||
  • !
if and if/else
1
2
3
4
let temperature = 100
if temperature >= 100 {
    print("The water is boiling")
}
1
2
3
4
5
6
7
8
var finished = 2
if finished == 1 {
    print("First place!") 
} else if finished == 2 {
    print("Second place!")
} else {
    print("Third place!")
}
Boolean values
1
2
3
4
var isSnowing = false
if !ifSnowing {
    print("It is not snowing")
}
Switch statement
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let numberOfWheels = 2
switch numberOfWheels {
    case 0:
        print("Missing wheels!")
    case 1:
        print("Unicycle")
    case 2:
        print("Bicycle")
    case 3:
        print("Tricycle")
    case 4:
        print()"Cars!")
    default:
        print("Too many wheels!")
}
1
2
3
4
5
6
7
let character = "z"
switch character {
    case "a", "e", "i", "o", "u":
        print("Vowels")
    default:
        print("consonant")
}
1
2
3
4
5
6
7
8
9
10
switch distance {
    case 0...9:
        print("It is close")
    case 10...99:
        print("A bit further")
    case 100..999: 
        print("Quite far out")
    default:
        print("Let's call a cab")
}

Exercises

Grade calculation using if-else

Write a Swift program that takes a student’s numerical grade (0 to 100) as input and outputs the corresponding letter grade based on the following criteria:

  • A: 90-100
  • B: 80-89
  • C: 70-79
  • D: 60-69
  • F: Below 60

Template playground code:

1
2
3
4
let grade = 95

//implement if-else logic here

Grade calculation using switch

Write a Swift program that takes a student’s numerical grade (0 to 100) as input and outputs the corresponding letter grade based on the following criteria:

  • A: 90-100
  • B: 80-89
  • C: 70-79
  • D: 60-69
  • F: Below 60

Template playground code:

1
2
3
4
let grade = 95

//implement switch logic here

Grade calculation on app
  • Create a new app using the pokemon app from the introduction lecture as template
  • Modify it such as as a user enter the numeric grade, the corresponding letter grade is displayed
  • Bonus: display the letter grade using images!