Method dispatch

The method declared in the class is passedV-tableFor schedulingYou can see from the figure above that the terminal writes oneclassEnter after runinitClassVTableIn order tovtableIn theNumber of elementsEnd the loop, get the method information, and thenContinuous storage to memoryamong

But byextensionThe added method then calls it, and it becomesDirect call

Check the assemblyThe call is already determinedaddress

Create a subclass of Person and look at the SIL fileYou can seeextensionIn theMethods are not stored in the Vtable, the subclass vtable does not inherit,

Pointer to the

Pointers in Swift are divided intoSpecify a type pointer.A raw pointer of an unspecified data type is also called a native pointer Pointer to theMemory managementIs the need toManual managementThe pointer is inUsing theNeed to beHand release

Native pointer case

Note: When adding data to the memory loop, be careful to store itType of dataandThe step size of typeOtherwise, it will lead to depositData confusion error

Type a pointer

withUnsafePointerThe second argument is aClosure expressionThrough therethrowsthrowresultSo can be abbreviated

Let p = withUnsafePointer(to: &age) {$0} let p = withUnsafePointer(to: &age) {PTR in return PTR}Copy the code

pointeeIs the pointer to getvalue

When creating UnsafeMutablePointer with allocate, note that initialize and deinitialize must occur at the same time. Count in deinitialize must be the same as capacity in allocate. Finally, you need to manually deallocate

Pointer to the binding

Notice hereadvancedThe mobileStep lengthAnd here is theType a pointerAlready knownType of pointerAnd know hisStep lengthSo no needSpecify the step size, write directlyThe number of offset stepsIt is good

The instance is bound to the structure

Primitive pointer conversion

Structure property pointer

withMemoryRebound

In the withMemoryRebound scope, PTR is UInt64, p is still Int after it is out of scope

bindMemory Change the type of memory binding If it was not previously bound, it is bound for the first time, and if it was bound, it is rebound to the new type
assumingMemoryBound Assuming memory binding No need to check, it is already the specified type
withMemoryRebound Temporarily change the memory binding type