Swift 4语言中内存管理功能及用法通过自动引用计数(ARC)来处理。 ARC用于初始化和取消初始化系统资源,从而在不再需要实例时释放类实例使用的内存空间。 ARC会跟踪有关代码实例之间关系的信息,以便有效地管理内存资源。
init()
创建新类实例时的信息。deinit()
自动释放内存空间,以进行进一步的类实例存储和检索。deinit()
仅应用于那些未使用的实例。class StudDetails { var stname: String! var mark: Int! init(stname: String, mark: Int) { self.stname = stname self.mark = mark } deinit { print("Deinitialized \(self.stname)") print("Deinitialized \(self.mark)") } } let stname = "Swift 4" let mark = 98 print(stname) print(mark)
当使用playground 运行上述程序时,得到以下结果 -
Swift 4 98
class studmarks { let name: String var stud: student? init (name: String) { print("Initializing: \(name)") self.name = name } deinit { print("Deallocating: \(self.name)") } } class student { let name: String var strname: studmarks? init (name: String) { print("Initializing: \(name)") self.name = name } deinit { print("Deallocating: \(self.name)") } } var shiba: studmarks? var mari: student? shiba = studmarks(name: "Swift 4") mari = student(name: "ARC") shiba!.stud = mari mari!.strname = shiba
当使用playground 运行上述程序时,得到以下结果 -
Initializing: Swift 4 Initializing: ARC
类类型属性有两种方法可以解决强引用周期 -
这些引用用于使一个实例能够引用参考周期中的其他实例。 然后,实例可以参考每个实例而不是关注强引用循环。 当用户知道某个实例可能返回nil
值时,可指出使用弱引用。 当实例返回某些内容而不是nil
时,则使用无主引用声明它。
弱引用程序
class module { let name: String init(name: String) { self.name = name } var sub: submodule? deinit { print("\(name) Is The Main Module") } } class submodule { let number: Int init(number: Int) { self.number = number } weak var topic: module? deinit { print("Sub Module with its topic number is \(number)") } } var toc: module? var list: submodule? toc = module(name: "ARC") list = submodule(number: 4) toc!.sub = list list!.topic = toc toc = nil list = nil
当使用playground 运行上述程序时,得到以下结果 -
ARC Is The Main Module Sub Module with its topic number is 4
无主引用程序
class student { let name: String var section: marks? init(name: String) { self.name = name } deinit { print("\(name)") } } class marks { let marks: Int unowned let stname: student init(marks: Int, stname: student) { self.marks = marks self.stname = stname } deinit { print("Marks Obtained by the student is \(marks)") } } var module: student? module = student(name: "ARC") module!.section = marks(marks: 98, stname: module!) module = nil
当使用playground 运行上述程序时,得到以下结果 -
ARC Marks Obtained by the student is 98
当要为类实例属性和闭包的主体分配闭包以捕获特定实例时,可能会发生强引用周期。 对闭包的强引用由self.someProperty
或self.someMethod()
定义。 强引用周期用作闭包的引用类型。
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) />" } } init(samplename: String, text: String? = nil) { self.samplename = samplename self.text = text } deinit { print("\(samplename) is being deinitialized") } } var paragraph: HTMLElement? = HTMLElement(samplename: "p", text: "Welcome to Closure SRC") print(paragraph!.asHTML())
当使用playground 运行上述程序时,得到以下结果 -
<p>Welcome to Closure SRC</p>
当闭包和实例相互引用时,用户可以将闭包中的捕获定义为无主引用。 然后它不允许用户同时解除分配实例。 当实例有时返回nil
值时,用弱实例定义闭包。
class HTMLElement { let module: String let text: String? lazy var asHTML: () -> String = { [unowned self] in if let text = self.text { return "<\(self.module)>\(text)</\(self.module)>" } else { return "<\(self.module) />" } } init(module: String, text: String? = nil) { self.module = module self.text = text } deinit { print("\(module) the deinit()") } } var paragraph: HTMLElement? = HTMLElement(module: "Inside", text: "ARC Weak References") print(paragraph!.asHTML()) paragraph = nil
当使用playground 运行上述程序时,得到以下结果 -
<Inside>ARC Weak References</Inside> Inside the deinit()