Last active
August 29, 2015 14:23
-
-
Save idStar/2504696086b48d3bd29e to your computer and use it in GitHub Desktop.
Intermediate Swift Tutorial on RayWenderlich.com: Solution to Exercise in Part 9: Memory Managment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Part 09: Memory Management | |
// Swift 2.0 (Xcode 7 Beta 1) | |
// "Uber" Challenge Exercise from Ray Wenderlich's "Intermediate Swift Series", Part 9. | |
// Series available here: http://www.raywenderlich.com/video-tutorials#swift | |
// Solution created by Sohail Ahmed, twitter: @idStar, blog: http://sohail.io | |
class Conference { | |
var name:String | |
var instructors:[Instructor] = Array<Instructor>() | |
var students:[Student] = Array<Student>() | |
var sessions:[Session] = Array<Session>() | |
init(name: String) { | |
self.name = name | |
} | |
deinit { | |
print("Conference de-initialized: \(name)") | |
instructors.removeAll() | |
students.removeAll() | |
sessions.removeAll() | |
} | |
} | |
class Instructor { | |
var name:String = "" | |
weak var session:Session? | |
init(name:String, session: Session) { | |
self.name = name | |
self.session = session | |
} | |
deinit { | |
print("Instructor de-initialized: \(name)") | |
session = nil | |
} | |
} | |
class Session { | |
var name:String = "" | |
weak var instructor:Instructor? | |
var students:[Enrollment] = Array<Enrollment>() | |
init(name: String, instructor:Instructor?) { | |
self.name = name | |
self.instructor = instructor | |
} | |
convenience init(name: String) { | |
self.init(name: name, instructor: nil) | |
} | |
deinit { | |
print("Session de-initialized: \(name)") | |
instructor = nil | |
students.removeAll() | |
} | |
} | |
class Student { | |
var firstName:String = "" | |
var lastName:String = "" | |
var sessions:[Enrollment] = Array<Enrollment>() | |
init(firstName:String, lastName:String) { | |
self.firstName = firstName | |
self.lastName = lastName | |
} | |
deinit { | |
print("Student de-initialized: \(firstName) \(lastName)") | |
sessions.removeAll() | |
} | |
} | |
struct Enrollment { | |
weak var student:Student? | |
weak var session:Session? | |
init(student:Student, session:Session) { | |
self.student = student | |
self.session = session | |
self.student?.sessions.append(self) | |
self.session?.students.append(self) | |
} | |
} | |
// Conference | |
var rwDevCon:Conference? = Conference(name:"RW Dev Con") | |
// Sessions | |
var spriteKitSession:Session? = Session(name: "Introduction to Sprite Kit") | |
var adaptiveLayoutSession:Session? = Session(name: "Using Adaptive Layout") | |
var watchKitSession:Session? = Session(name: "Introducing WatchKit") | |
rwDevCon?.sessions = [spriteKitSession!, adaptiveLayoutSession!, watchKitSession!] | |
// Instructors | |
var ray:Instructor? = Instructor(name: "Ray Wenderlich", session: spriteKitSession!) | |
var greg:Instructor? = Instructor(name: "Greg Heo", session: adaptiveLayoutSession!) | |
var mic:Instructor? = Instructor(name: "Mic Pringle", session: watchKitSession!) | |
rwDevCon?.instructors = [ray!, greg!, mic!] | |
// Students | |
var student1:Student? = Student(firstName: "Reinhold", lastName: "Messner") | |
var student2:Student? = Student(firstName: "Gerlinde", lastName: "Kaltenbrunner") | |
var student3:Student? = Student(firstName: "Jrezy", lastName: "Kukuczka") | |
var student4:Student? = Student(firstName: "Ed", lastName: "Viesturs") | |
rwDevCon?.students = [student1!, student2!, student3!, student4!] | |
// Assigning Students to Sessions: | |
Enrollment(student: student1!, session: spriteKitSession!) | |
Enrollment(student: student1!, session: watchKitSession!) | |
Enrollment(student: student2!, session: adaptiveLayoutSession!) | |
Enrollment(student: student3!, session: watchKitSession!) | |
Enrollment(student: student4!, session: watchKitSession!) | |
// Next, we'll set our local, optional variables to nil, so we can see that they | |
// actually get released from the Conference object graph when our conference, rwDevCon, | |
// is finally released. | |
// Remove: Sessions | |
spriteKitSession = nil | |
adaptiveLayoutSession = nil | |
watchKitSession = nil | |
// Remove: Instructors | |
ray = nil | |
greg = nil | |
mic = nil | |
// Remove: Students | |
student1 = nil | |
student2 = nil | |
student3 = nil | |
student4 = nil | |
// Only once we set the following to nil, do we see the cascade of de-initializers | |
// from the object graph that it contains. Comment this line out to see the change: | |
rwDevCon = nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment