Swift also uses automatic reference counting (ARC) to manage memory

Swift includes strong(strong reference type), unowned(unreferenced reference type), and weak(weak reference type).

Strong references & Undirected References (unowned)

View swift source code analysis of strong references and ownerless references

Memory distribution is as follows unowned is ranked 1 through 31, and strong is ranked 33 through 62

The code analysis

RefCount is0x0000000600000002, the reference count is 3, and view the SIL fileYou can see the assemblyView in the source codeswift_retain Breakpoints debug the above codeThe reference count is set to 1 when the instance variable is createdAnd the memory distribution diagram aboveechoThere is also a function that evaluates the reference count

When you look at the reference count with CFGetRetainCount, the reference count is +1

A weak reference

As you can see from the figure above, the weak modified object is an optional type. The original p is not an optional type, but if you look at refCount, you can’t see what the reference count isAfter adding weak, swift_weakInit was called, and swift_weakInit was searched in swift source code0xC0000000200A873e is the address of HeapObjectSideTableEntry, which can be viewed from the calculationThe reference count was offset 3 bits to the right. The calculator was offset 3 bits to the left to get 0x1005439F0As can be seen from the figure, the address of P is output, 0x0000000200000002 is strong reference count, 0x0000000000000002 is weak reference count

Circular references to closures

If you define a closure with no arguments and no return value, you can find that it is consistent with blocks in oc. Changes to variables inside the closure change the values of the original variables outsideIn the test method, the instance variable is required to reference, and cannot be released. After running, the deinit method cannot be printed, creating a circular referenceAs shown in the figure above, weak and unowned can be solved, but the value modified by unowned will be assumed to have a value throughout the running cycle, so there will be wild Pointers, so to ensure that the life cycle is controllable,

[unowned p] in [weak p] in This syntax is collectively referred to as a capture list in Swift, and must be defined before the argument list, with square brackets, and must be accompanied by the in keywordConstants in the captured list are initialized by looking for the same constant name based on the context. Constants in the captured list are value copies and cannot be modified

RunTime

The API in oc Runtime can be called in a pure Swift classBut this is meaningless, oc can not be called, need to let the attributes and methods in SWIFT leak out for OC call need to add keywordsView the swiftDemo-swift. h fileIt’s at the bottom of the fileYou can see the properties and methods that leak outThe swiftDemo-swift. h header is introduced into oc so that Swift class properties and methods can be called

  • Swift is a static language. For pure Swift classes, there are no dynamic features. Methods and attributes without any modifiers do not have runtime features, but are simply v-table calls
  • For pure Swift classes, methods and attributes with @objc added can be retrieved by the runtime API, but not by the OC class
  • For Swift, which inherits from Nsobject, the @objc keyword must be added before life if the OC class is to be called, and the dynamic keyword must be added before the method if method swapping is to be used, otherwise it will not be dynamic if it leaks out

Check out the source code for class_copyMethodListCLS is LGTeacher, which means CLS is swift class. In OC, data stores the information of the class and enters dataThere is a default base class for Swift, and the default base class is SwiftObject, SwiftObject in the Swift source codeYou can see that SwiftObject inherits NSObject, and it also contains ISA and refCount, TargetAnyClassMetadata inherits from TargetHeapMetaData, TargetHeapMetaData has only one kind attribute, and TargetAnyClassMetadata has four attributes: ISA, Superclass, cacheData, and data (bits)

anyobject

AnyObject

AnyObject represents the instance object of the class, the type of the class, and only the protocol that the class complies with. If you are not sure what type you can use AnyObjectIf a structure complies with the JSONMap protocol, an error is reported Only classes adhere to the protocol

any

Any can store any type in an array. For example, function, Optional, and anyObject are subsets of any

anyclass

Anyclass represents the type of any instance

T.TYPE T.SELF

When t is an instance, t. elf stores the instance object. When t is a class, t. elf stores the class itself

type(of:)

Type (of:) is used to get the dynamic type of a classSuch asThe compiler passes test of type person in the compiler task, which is actually of type phperson. You can use type(of:) to retrieve the actual typeDeclare a Person protocol, make PHPerson comply with the Person protocol, create an instance of PHPerson and create an instance of the Person protocol type, pass it in test and print it out as a Person, butWhen the test method is generic and the value is generic, the printing changes,The value of type(of:) must be strongly changed to any. In some cases, it is not possible to obtain the desired type with protocols and generics, so it can be strongly changed to any