Complete list of swift short hand

This tutorial has been updated for X code 7.3 & swift 2.2

Some of the long swift code is used very frequently without modification. Because they are used often there are short hand version of it. They achieve same result with less writing of code. Here I am writing all the short hands I know till today & I will be adding more short hands as I learn them.

For it statement very frequently its checked if bool is true or false

var isItTrue = true

if isItTrue == true {
print(“bool = true”)
}

// same as

if isItTrue {
print(“bool is true”)
}

bool is true

var isItFalse = false

if isItFalse == false {
print(“bool = false”)
}

// same as

if !isItFalse {
print(“bool is false”)
}

bool is false

If else statement itself is used very frequently & here is short hand for that

var x = 0

if x == 5 {
x = 0
}
else {
x = 5
}
print(x)

// same as

x = x == 5 ? 0 : 5
print(x)

if else output

Adding or subtracting value from variable
var x = 0

x = x + 3
println(x)

// same as

x += 3
println(x)

x += 3 output

var x = 6

x = x - 3
print(x)

// same as

x -= 3
print(x)

x -= 3 output

Creating empty array of object

var city = Array<String>()

// same as

var name = [String]()

Adding object to array

var city = [String]()

city.append(“New York”)
city.append(“London”)
city.append(“Paris”)
city.append(“Tokio”)

print(city)

// same as

var name = [String]()

name += [“John”, “Casey”, “Mike”, “Richard”]

print(name)

adding object to array

Subtracting objects from array

var city = [“New York”, “London”, “Paris”, “Tokio”]

var i = city.count - 1

city.removeAtIndex(i)

print(city)

// same as

city.removeLast()

print(city)

remove last

var city = [“New York”, “London”, “Paris”, “Tokio”]

print(city)
city.removeAtIndex(0)
print(city)
city.removeAtIndex(0)
print(city)
city.removeAtIndex(0)
print(city)
city.removeAtIndex(0)
print(city)

// same as

var name = [“John”, “Mike”, “Dave”, “Richard”]

print(name)
name.removeAll(keepCapacity: false)
print(name)

remove all

Replacing objects from array

var city = [“New York”, “London”, “Paris”, “Tokio”, “Bejing”]

print(city)
city[0] = “Boston”
print(city)
city[1] = “Manchester”
print(city)
city[2] = “Nice”
print(city)
city[3] = “Hiroshima”
print(city)

// same as

var name = [“John”, “Mike”, “Richard”, “Jack”, “Casey”]

print(name)
name[0…3] = [“Roger”, “Jay”, “Justin”, “Brad”]
print(name)

replacing object at range

var city = [“New York”, “London”, “Paris”, “Tokyo”]

print(city)
city[0] = “Boston”
print(city)
city[1] = “Manchester”
print(city)
city[2] = “Nice”
print(city)
city[3] = “Hiroshima”
print(city)

// same as

var name = [“John”, “Mike”, “Richard”, “Jack”]

print(name)
name[0..<name.count] = [“Roger”, “Jay”, “Justin”, “Brad”]
print(name)

replace all objects from array

Looping through array with for loop

var city = [“New York”, “London”, “Paris”, “Tokyo”]

for var i = 0; i < city.count; i++ {
print((i, city[i]))
}

// same as

for name in enumerate(city) {
print(name)
}

looping through array with index

var city = [“New York”, “London”, “Paris”, “Tokyo”]

for var i = 0; i < city.count; i++ {
print(i)
}

// same as

for (index, name) in enumerate(city) {
print(index)
}

looping through array index only

var city = [“New York”, “London”, “Paris”, “Tokyo”]

for var i = 0; i < city.count; i++ {
print(city[i])
}

// same as

for (index, name) in enumerate(city) {
print(name)
}

looping through array

Looping through numbers

for var i = 0; i <= 5; i++ {
print(i)
}

// same as

for number in 0…5 {
print(number)
}

looping through numbers

Looping through dictionary

var age = [“Mom”: 30, “Dad”: 33, “Son”: 2, “Daughter”: 4]

var x = age[“Mom”]!
print(“Mom”, “(x)”)
x = age[“Dad”]!
print(“Dad”,“(x)”)
x = age[“Son”]!
print(“Son”, “(x)”)
x = age[“Daughter”]!
print(“Daughter”, “(x)”)

// same as

for name in age {
print(name)
}

looping through dictionary with key

var age = [“Mom”: 30, “Dad”: 33, “Son”: 2, “Daughter”: 4]

var x = age[“Mom”]!
print(“Mom”)
print(“(x)”)
x = age[“Dad”]!
print(“Dad”)
print(“(x)”)
x = age[“Son”]!
print(“Son”)
print(“(x)”)
x = age[“Daughter”]!
print(“Daughter”)
print(“(x)”)

// same as

for (key, name) in age {
print(key)
print(name)
}

looping through dictionary

Multiple if else statements combined with switch

var i = 0

if i == 0 {
print(“i is equal to zero”)
}
else if i == 1 {
print(“i is equal to one”)
}
else if i == 2 {
print(“i is equal to two”)
}
else {
print(“value of i is unknown”)
}

switch i {
case 0:
print(“i is equal to zero”)
case 1:
print(“i is equal to one”)
case 2:
print(“i is equal to two”)
default:
print(“value of i is unknown”)
}

switch output
Download Full list of short hands

Total 3 Votes
0

Tell us how can I improve this tutorial?

+ = Verify Human or Spambot ?