Member-only story
iOS Developers Should Avoid These 5 Common Mistakes
We’ve all made them
4 min readMar 3, 2023
Memory management
We all want to avoid user frustration when creating our Apps. Retain cycles can cause crashes and slow performance for the end user, none of which is good to say the least. Instruments is a great tool to track these issues in existing (or new!) code.
In the following code (in a Playground, or similar) deinit
isn’t called for either the Tutorial
or Student
class. The reason? It’s a memory cycle.
class Tutorial {
private var students = [Student]()
func enroll(_ student: Student) {
students.append(student)
}
init() {
print ("Tutorial initialized")
}
deinit {
print ("Tutorial deinitialized")
}
}
class Student {
private var tutorial : Tutorial
private var name : String
init(tutorial: Tutorial, name: String) {
print ("Tutorial initialized")
self.tutorial = tutorial
self.name = name
}
deinit {
print ("Tutorial deinitialized")
}
}
var computing : Tutorial? = Tutorial()
var dave : Student? = Student(tutorial: computing!, name: "Dave")
computing?.enroll(dave!)
computing = nil
dave = nil