(Summary of articles on underlying principles of iOS)

(iOS)

The strong & Copy & Weak attribute found in CPP files compiled by Clang differs in the underlying code compiled

Strong & Copy & Weak Underlying analysis

  • In LGPerson we define two properties, which are labeled copy and strong, respectively

  • withclangwillmain.mFile compilation intomain.cpp, and find copy andstrongOf the properties of the modifiedsetThe methods are different

So the question is, why does the copy property use objc_setProperty, but the strong property doesn’t?

  • Search in LLVM"Objc_setProperty, find as shown belowgetOptimizedSetPropertyFnIn the method

You can see here that the returns are different for different modifiers

  • If it isatomic & copyModified, name isobjc_setProperty_atomic_copy
  • If it isAtomic with no copyModified, name isobjc_setProperty_atomic
  • If it isnonatomic & copyModified, name isobjc_setProperty_nonatomic_copy
  • The other remaining combinations, i.eNonatomic, nonatomic & strong, nonatomic & weakAnd so on, the name forobjc_setProperty_nonatomic

The above names correspond to the following methods in the OBJC-781 source code

And then through assembly debugging, it all ends up in objc_storeStrong

  • The copy attribute assembles the debugging results

  • The strong attribute assembles debugging results

  • Search in source codeobjc_storeStrong, there are the following source code, mainly alsoRetain the new value, release the old value
void objc_storeStrong(id *location, id obj) { id prev = *location; if (obj == prev) { return; } objc_retain(obj); //retain new value *location = obj; objc_release(prev); // Release old values} Copy the codeCopy the code
  • LLVM compiles source searchobjc_storeStrongTo findEmitARCStoreStrongCallMethod, as shown in the figure below, found that the policies of the copy and strong decorated properties were inconsistent
  • Search in the LLVMEmitARCStoreStrongCallMethods,GenerateCopyHelperFunctionMethod is called, and you find different handling of strong and weak here

BlockCaptureEntityKind has the following enumerated values and meanings

- If it is weak, execute the 'EmitARCCopyWeak' method, as shown below, and the underlying call to weak is' objc_initWeak '! [EmitARCCopyWeak underlying compiler] (HTTP: / / https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/a25990df8a65483d97508f0d7595d638~tplv-k3u1fbpfcp -zoom-1. Image) - If strong, execute 'EmitARCStoreStrongCall' to copy the codeCopy the code

conclusion

  • copyandstrongThe inconsistency in the underlying compilation of the decorated attributes is mainly the result of the different handling of them in LLVM.copyThe assignment of is byobjc_setProperty, while strong is assigned throughSelf + memory translation(i.e. the pointer is shifted to the position where the name is, and then assigned), and then restored tostrongtype
  • strong & copyCall at the bottom levelobjc_storeStrongThat is the essenceThe new value is retain, the old value is release
  • weakCall at the bottom levelobjc_initWeak

Supplementary knowledge: Type Encoding & Property Type String

Type Encoding- Official document

Property Type String- Official document

Method signatures in Clang

Type encoding

What do these characters mean in the compiled method list in Clang

@ @ 0:8 16, for example

  • @16 means the return string takes 16 bytes — the second @ takes 8 bytes, and sel takes 8 bytes

    • The first @ indicates the return value

    • 16 indicates the total number of bytes occupied 16 bytes

    • The second @ : the first argument

      • Id — @ Configuration type
      • typedef struct objc_object *id
    • 0 — let’s start at 0 0 minus 8

    • : — represents sel, the method number

    • 8-8-16

  • V — void in v24@0:8@16 returns no value

For more information, see the following list on the website

Attribute of a clang compiled attribute

Clang compiles that output propertiesattribute, can also be passedproperty_getAttributesMethods to obtain

  • Tsaidtype
  • @saidVariable types
  • Csaidcopy
  • Nsaidnonatomic
  • VsaidvariableVariables, that is, underlined variables_nickName

For more information, see the following list on the website