Swift Tutorial - Tutorialspoint

(backadmin) #1
unowned let stname: student

init(marks: Int, stname: student) {
self.marks = marks
self.stname = stname
}

deinit { println("Marks Obtained by the student is \(marks)") }
}

var module: student?
module = student(name: "ARC")
module!.section = marks(marks: 98 , stname: module!)
module = nil

When we run the above program using playground, we get the following result:


ARC


Marks Obtained by the student is 98

Strong Reference Cycles for Closures


When we assign a closure to the class instance property and to the body of the closure to
capture particular instance strong reference cycle can occur. Strong reference to the
closure is defined by 'self.someProperty' or 'self.someMethod()'. Strong reference cycles
are used as reference types for the closures.


class HTMLElement {
let samplename: String
let text: String?

lazy var asHTML: () - > String = {
if let text = self.text {
return "<\(self.samplename)>\(text)</\(self.samplename)>"
} else {
return "<\(self.samplename) />"
}
}
Free download pdf