1. Swift memory destruction timing
// Swift5 memory destruction timing// Memory destruction timing of reference typeclass ClassDemo { var a = "value a" deinit { // The instance is released print("deinit class a") } } // nullable typevar ins1: ClassDemo? = ClassDemo() var ins2 = ins1 var ins3 = ins2 ins1 = nil // Cancel the ins1 referenceins2 = nil // Cancel ins2 referenceprint(String(describing: ins3?.a)) // The instance referenced by ins3 here is still there, Optional("value a") // All references to the instance are cancelled, and the ClassA instance is destroyed hereins3 = nil // deinit class a
2. Swift One-way Quotation
// Swift5 One-way Quotationclass ClassA { deinit { print("deinit ClassA") } func foo() { print("func foo in ClassA") } } class ClassB { // Examples of ClassA are referenced here var ins: ClassA? init(ins: ClassA?) { = ins } deinit { print("deinit ClassB") } } var clzA: ClassA? = ClassA() var clzB: ClassB? = ClassB(ins: clzA) // The memory referenced by clzA here is not releasedclzA = nil // You can still call the foo method of the clzA instance in clzBclzB?.ins?.foo() // func foo in ClassA // At this time, the ClassB instance is released, and no longer reference points to ClassA's memory is also releasedclzB = nil // deinit ClassB \n deinit ClassA
3. Swift circular reference
// Swift5 circular referenceclass ClassC { var insD: ClassD? deinit { print("deinit ClassC") } func foo() { print("func foo in ClassC") } } class ClassD { // Example of ClassC is referenced here var insC: ClassC? init(ins: ClassC?) { = ins } deinit { print("deinit ClassD") } } var clzC: ClassC? = ClassC() var clzD: ClassD? = ClassD(ins: clzC) clzC?.insD = clzD // Here the memory referenced by clzC is not released, and the corresponding instance is referenced by clzD's insCclzC = nil // You can still call the foo method of the insC instance in clzDclzD?.insC?.foo() // func foo in ClassC // At this time, the instance of clzD is still referenced by clzC's insD, and neither clzC nor clzD instances are releasedclzD = nil
4. Swift Weak Reference Solve Circular Reference Problem
// Swift5 uses weak references to resolve circular referencesclass ClassE { // Weak quote weak weak var insF: ClassF? deinit { print("deinit ClassE") } func foo() { print("func foo in ClassE") } } class ClassF { // Example of ClassE is referenced here var insE: ClassE? init(ins: ClassE?) { = ins } deinit { print("deinit ClassF") } } var clzE: ClassE? = ClassE() var clzF: ClassF? = ClassF(ins: clzE) clzE?.insF = clzF // Here the memory referenced by clzE has not been released, and the corresponding instance is referenced by clzF's insEclzE = nil // You can still call the foo method of the insE instance in clzFclzF?.insE?.foo() // func foo in ClassE // At this time, the instance of clzF is weakly referenced by clzE's insF and will be destroyed. Both clzE and clzF instances can be releasedclzF = nil // deinit ClassF \n deinit ClassE
5. Swift has no master reference, for type non-Optional
// Swift5 has no master reference, for type non-Optionalclass ClassG { // Unowned reference unowned assumes that the property is not nil unowned var insH: ClassH init(ins: ClassH) { = ins } func foo() { print("func foo in ClassG") } deinit { print("deinit ClassG") } } class ClassH { // Example of ClassE is referenced here var insG: ClassG? deinit { print("deinit ClassH") } } var clzH: ClassH? = ClassH() var clzG: ClassG? = ClassG(ins: clzH!) clzH?.insG = clzG // The memory referenced by clzG here has not been released, and the corresponding instance is referenced by clzH's insGclzG = nil // You can still call the foo method of the insG instance in clzHclzH?.insG?.foo() // func foo in ClassG // At this time, the instance of clzH is unregistered by clzG's insH and will be destroyed. Both clzG and clzH instances can be releasedclzH = nil // deinit ClassH \n deinit ClassG
6. Circular references generated by Swift closures
// Circular reference generated by Swift5 closureclass ClassJ { var field = "field j" lazy var closure: () -> Void = { print() } deinit { print("deinit ClassJ") } } var objJ: ClassJ? = ClassJ() objJ?.closure() // Because the closure refers to the class member attribute, the instance cannot be released, which in turn causes the closure to be released, resulting in the cyclic referenceobjJ = nil // The information in deinit is not printed here
7. Swift solves circular references generated by closures
// Swift5 solves circular references generated by closuresclass ClassK { var field = "field k" lazy var closure: () -> Void = { // Use capture list to convert self without reference [unowned self] () -> Void in print() } deinit { print("deinit ClassK") } } var objK: ClassK? = ClassK() objK?.closure() objK = nil // deinit ClassK
8. Swift custom exception type
// Swift5 custom exception typeenum CustomError: Error { case ErrorOne case ErrorTwo case ErrorThree } print("error") //throw // The thrown exception is not caught and will terminate, and complete will not be printedprint("complete")
9. Swift do-catch catches exceptions, try executes a function that throws exceptions
// Swift5 uses do-catch to catch exceptions, and try executes a function that throws exceptions// Throw an exception through the functionfunc funcError() throws -> String { throw } // Use do-catch to catch exceptionsdo { // Use try to execute functions that may throw exceptions try funcError() } catch { print("ErrorOne") } catch { print("ErrorTwo") } catch { print("ErrorThree") } // Use try? to map the result of the function execution to Optional typelet result = try? funcError() if (result == nil) { print("exec failed") } // try! Forcefully terminates the exception's delivery. If an exception occurs, the program will be interrupted.// try! funcError()
10. Swift function delay execution structure
// Swift5 function delay execution structure: avoid throwing exceptions and ensure that certain necessary code blocks are executed, such as releasing resourcesfunc lazyFunc() throws -> Void { defer { // The function will be executed at the end print("lazy part of func") } print("exec lazyFunc") throw } // exec lazyFunc // lazy part of func try? lazyFunc()
GitHub source code:Reference&
This is the article about Swift listing the specific codes for memory management and exception handling. For more related contents for Swift memory management and exception handling, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!