内存管理函数及其用法通过自动引用计数(ARC)以Swift 4语言处理。 ARC用于初始化和取消初始化系统资源,从而在不再需要时释放类使用的内存空间。 ARC跟踪有关我们的代码之间的关系的信息,以有效地管理内存资源。

ARC函数

  • 每次通过init()创建新的类时,ARC每次都会分配一块内存来存储信息。

  • 有关类型及其值的信息存储在内存中。

  • 当不再需要类时,它会通过deinit()自动释放内存空间,以用于进一步的类存储和检索。

  • ARC会跟踪当前引用的类的属性,常量和变量,以便将deinit()仅应用于那些未使用的。

  • ARC维护对这些类属性,常量和变量的"强引用",以在当前使用类时限制释放。

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

ARC循环实例

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

ARC弱引用

类类型属性有两种解决强引用循环的方法-

  • Weak References
  • Unowned References

Weak Reference程序

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

Unowned Reference 程序

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 "<\(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())

运行上述程序时,我们得到以下输出-

<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

运行上述程序时,我们得到以下输出-

<Inside>ARC Weak References</Inside>
Inside the deinit()

参考链接

https://www.learnfk.com/swift/swift-arc-overview.html