These two months to see some of the lower oc things, or there are many do not understand, in order to deepen the impression, recorded as follows:
1, the reference count value of object A is stored in a global hash list (not considered tagged pointer optimization), the address of a &a is key, the reference count value minus 1 is value. When a is retain, the corresponding reference count value is found in the global hash table according to &a, plus 1.
2. All objects modified by __weak are stored in a global hash list, for example a object, if __weak obj1 = A; __weak Obj2 = A, the hash list is keyed with &a, and an array-like object is value, which stores obj1 and Obj2. When the A object is destroyed, the corresponding array is found in the hash table with the value &a, the obj1 and Obj2 are taken out, and the pointer is emptied to nil.
3. The __weak modifier holds only the weak reference to the object, which can be discarded and dangerous when accessing the weak reference object. So when we access the __weak decorated object, the system automatically registers it in the autorelease, which guarantees the life cycle of the object during use. Every access to a weak reference object registers the object in an auto-free pool once, wasting performance. Therefore, the following style is now popular. To ensure that the object must exist during block execution, and no longer accesses the object that repeatedly accesses the weak reference adornment, avoid repeatedly registering the object to the auto-free pool.
1NSObject *obj = [NSObjectNew];3__weaktypeof(obj) Weakobj =obj;5 void(^BLK) () = ^{7__strongtypeof(weakobj) Strongobj =obj;9 if(strongobj) {TenNSLog (@"%@", strongobj); One } A }; -Blk ();
4. Code Snippet
{ id obj = [Nsmutablearray array]; }
The above compiler simulation runtime code can be converted to the following:
ID obj = objc_msgsend (Nsmutablearray, @selector (array)); Objc_retainautoreleasedreturnvalue (obj); Obj_release (obj)
Interpretation: Array class Nsmutablearray send @selector (array) message Create object Obj,retain object Let it survive, quit scope destroy
The implementation of the class method + (ID) array is
+ (ID) array{ return [[Nsmutablearray alloc] init];}
The analog conversions are as follows
+ (ID) array{ id obj = objc_msgsend (Nsmutablearray, @selector (array)); Objc_msgsend (obj, @selector (init)); return objc_autoreleasereturnvalue (obj);}
Interpretation: Create an Array object obj, drop it into the auto-release pool and return
Looking at the above parsing, it is found that the array object is created and placed in the auto-free pool, then retain, at which point the reference count value is 2. Exits the scoped strong reference pointer, obj is disposed, and the reference counter is reduced by one. When this runloop is finished, the auto-free pool is emptied, the array object is released again, and the reference counter is destroyed by the 0 array. From the code point of view, the array into the auto-release pool with retain completely unnecessary, the generated object directly strong pointer hold, exit scope destroy.
The following objc_autoreleasereturnvalue (): Replace with symbol A, objc_retainautoreleasedreturnvalue (): Replace with symbol b
Runtime optimization: A and b always appear in pairs, a always precedes B. The methods A and B have been specially treated so that they do not put the object into the auto-release pool and retain the object in the case above, but dynamically judge that when a method is found followed by the B method, the A method no longer puts the object into the auto-free pool, but instead sets a flag to flag to Yes. For the B method, check the flag bit flag, if it finds its bit yes, it will no longer retain the object, but simply resets the flag to flag to no and saves two operations. The pseudo code is as follows:
1 StaticBOOL flag =NO;2 IDObjc_autoreleasereturnvalue (obj) {3 if(@"Followed by the B method.") {4Flag =YES;5}Else{6 [obj autorelease];7 }8 returnobj;9 }Ten One voidObjc_retainautoreleasedreturnvalue (obj) { A if(flag) { -Flag =NO; -}Else{ the [obj retain]; - } -}
5. Runtime General Purpose
A, data binding, main functions Objc_setassociatedobject and objc_getassociatedobject, mainly used for classification attribute expansion
b, to view the private member variable of the system class is already private method, can be KVC assigned to the system private variable.
C, black box method swizzling, follow-up system method, such as the Viewwillappear and Viewdiddisappear methods of all controllers to add friends of the league statistical Method, statistical page dwell time
6, KVO Realization Principle: Observe the progress property of a object, the system dynamically creates subclass B of a, points the Isa pointer of A to B, overrides the Setprogress method of object B, and notifies the observer of the corresponding method by issuing the following notification.
-(void) setpregress: (cgfloat) progress{ [self willchangevalueforkey:@ "Progress "]; = progress; [Self Didchangevalueforkey: @" Progress " ];}
7, synchronous thread deadlock. The following code performs a deadlock on the main thread
Dispatch_sync (Dispatch_get_main_queue (), ^{ NSLog (@ " deadlock "); });
The sync function dispatch_sync the thread until the block returns. If the current code is executed on the main thread, the main thread will be blocked until the block returns. The block happens to be executed in the main thread, and the main thread is blocked, so the Blcok will not execute, the block will not execute and will not return, so the main thread has been blocked. Cause a deadlock.
OC in-depth knowledge points