📅  最后修改于: 2020-12-25 04:38:41             🧑  作者: Mango
内存管理功能及其用法通过自动引用计数(ARC)以Swift 4语言处理。 ARC用于初始化和取消初始化系统资源,从而在不再需要实例时释放类实例使用的内存空间。 ARC跟踪有关我们的代码实例之间的关系的信息,以有效地管理内存资源。
每次通过init()创建新的类实例时,ARC都会分配一块内存来存储信息。
有关实例类型及其值的信息存储在内存中。
当不再需要该类实例时,它将通过deinit()自动释放内存空间,以用于进一步的类实例存储和检索。
ARC跟踪当前引用的类实例的属性,常量和变量,以便将deinit()仅应用于那些未使用的实例。
ARC维护对这些类实例属性,常量和变量的“强大引用”,以在当前使用类实例时限制释放。
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)
当我们使用游乐场运行上述程序时,我们得到以下结果-
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
当我们使用游乐场运行上述程序时,我们得到以下结果-
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
当我们使用游乐场运行上述程序时,我们得到以下结果-
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
当我们使用游乐场运行上述程序时,我们得到以下结果-
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 "\(text)\(self.samplename)>"
} else {
return ""
}
}
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())
当我们使用游乐场运行上述程序时,我们得到以下结果-
Welcome to Closure SRC
当闭包和实例相互引用时,用户可以将闭包中的捕获定义为未拥有的引用。然后,它将不允许用户同时取消分配实例。当实例有时返回“ nil”值时,用弱实例定义闭包。
class HTMLElement {
let module: String
let text: String?
lazy var asHTML: () -> String = {
[unowned self] in
if let text = self.text {
return "\(text)\(self.module)>"
} else {
return ""
}
}
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
当我们使用游乐场运行上述程序时,我们得到以下结果-
ARC Weak References
Inside the deinit()