Swift Syntax

Claudia Maciel
1 min readJan 28, 2021

I find that sometimes I forget the correct syntax for items and here are a few of my most common ones that I have searched this past week.

Switch Statement Syntax

switch <item to compare to> {

case1:
statement
case2:
statement
case 3:
statement
default:
statement
}

Ternary Operator

<condition statement> ? <true> : <false>

Range

Inclusive: 1…10
Non-inclusive: 1..<10

While loop

var i = 0

while ( i < 10) {
print(i)
i += 1
}

Random #’s

Int(arc4random( 0 % 6) + 1 //1 through 6

Assign a default value to a parameter

func calculateTip(priceOfMeal: Double, tip: Double = 0.15) {
}

Internal and External Parameter Names

func addValues ( value1 x: Int, value2 y: Int) -> Int {
// internal use x and y
return x + y
}

//external use value1 and value2
addValues(value1: 5, value2: 10)

Computed Property
to change or create a property based on other properties

struct Beaker {
let volumeMilliliters: Double
//convert to ounces
var volumeOunces: Double {
return volumeMilliliters * 0.033814
}
}

--

--

Claudia Maciel

I am an iOS developer who loves to learn and share what I learn with those that are starting out.