ARC Explained

Value Types

var myAge = 20
var yourAge = myAge
myAge = 30if myAge == yourAge {
print("Both are same")
} else {
print("Both are different")
}

// Both are different

Reference Types

class Age: Equatable {

static func == (lhs: Age, rhs: Age) -> Bool {
lhs.value == rhs.value
}

var value = 20
}var myAge = Age()
var yourAge = myAge
myAge.value = 30if myAge == yourAge {
print("Both are same")
} else {
print("Both are different")
}
// Both are same

ARC

class Pet {
let name: String

init(name: String) {
self.name = name
print("Pet \(name) was initialised")
}
deinit {
print("Deallocating Pet \(name) ...")
}
}
class User {
let name: String
let pet: Pet
init(name: String, pet: Pet) {
self.name = name
self.pet = pet
print("User \(name) was initialised")
}
deinit {
print("Deallocating User \(name) ...")
}
}
func run() {
let pet = Pet(name: "Jacky")
let user = User(name: "Dave", pet: pet)
}
run()// Pet Jacky was initialised
// User Dave was initialised
// Deallocating User Dave ...
// Deallocating Pet Jacky ...

Retain Cycle

var owner: User?
per.owner = user
// Pet Jacky was initialised
// User Dave was initialised

Weak Reference

weak var pet: Pet?
// Pet Jacky was initialised
// User Dave was initialised
// Deallocating User Dave ...
// Deallocating Pet Jacky ...

Unowned Reference

unowned let pet: Pet

Retain Cycles With Closures

lazy var completeInformation: () -> String = {
return "My name: \(self.name) and my pet's name: \(self.pet.name)"
}
print(user.completeInformation())
// Pet Jacky was initialised
// User Dave was initialised
// My name: Dave and my pet's name: Jacky
// Deallocating Pet Jacky ...
lazy var completeInformation: () -> String = { [unowned self] in
return "My name: \(self.name) and my pet's name: \(self.pet.name)"
}

Conclusion

A Software Engineer with a passion for technology. Working as an iOS Developer @BBC

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

287 Hours

Reduce Cost and Increase Productivity with Value Added IT Services from buzinessware — {link} -

Docker Containers — Deep Dive

Immersive Technology — VR Is For Everyone

How to configure HTTP Client

Speech to Text using Google API

Blog | What’s the Difference Between a GIF and a Video?

Don’t Update Visual Studio Code

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Melvin John

Melvin John

A Software Engineer with a passion for technology. Working as an iOS Developer @BBC

More from Medium

Improve your architecture by abstract your code

Creating a macOS Statusbar Application

[Patterns] Abstract Factory

Abstract factory pattern

Struct vs Classes