(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
- with
clang
willmain.m
File compilation intomain.cpp
, and find copy andstrong
Of the properties of the modifiedset
The 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 belowgetOptimizedSetPropertyFn
In the method
You can see here that the returns are different for different modifiers
- If it is
atomic & copy
Modified, name isobjc_setProperty_atomic_copy
- If it is
Atomic with no copy
Modified, name isobjc_setProperty_atomic
- If it is
nonatomic & copy
Modified, name isobjc_setProperty_nonatomic_copy
- The other remaining combinations, i.e
Nonatomic, nonatomic & strong, nonatomic & weak
And 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 code
objc_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 search
objc_storeStrong
To findEmitARCStoreStrongCall
Method, as shown in the figure below, found that the policies of the copy and strong decorated properties were inconsistent - Search in the LLVM
EmitARCStoreStrongCall
Methods,GenerateCopyHelperFunction
Method 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
copy
andstrong
The inconsistency in the underlying compilation of the decorated attributes is mainly the result of the different handling of them in LLVM.copy
The 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 tostrong
typestrong & copy
Call at the bottom levelobjc_storeStrong
That is the essenceThe new value is retain, the old value is release
weak
Call 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_getAttributes
Methods to obtain
T
saidtype
@
saidVariable types
C
saidcopy
N
saidnonatomic
V
saidvariable
Variables, that is, underlined variables_nickName
For more information, see the following list on the website