A block is a special code block that captures the context. A block can access variables defined outside a block. When used in a block, it creates a copy for each scalar variable in its scope. If self owns a block and then changes the instance variables in the block, an error occurs. For example:
self.block = ^(NSString * self.aLabel.text = });
In this Code, self retains the block, and the block retains the self, which will lead to loop retention. Dangerous. If ARC is not used, you can use _ block and _ unsafe_unretained to copy an unretained reference copy.
__block safeSelf = self.block = ^(NSString * safeSelf.aLabel.text = __weak safeSelf = self; self.block = ^(NSString * safeSelf.aLabel.text = });
Before the emergence of arc, we were able to freely convert the CF * object into the NS * object, which is called self-bridging. After using arc, We need to specify a ownership transfer modifier. Currently, the modifiers provided by arc are: 1. _ bridge2. _ bridge_retained3. _ bridge_transfer the first modifier _ bridge is a normal conversion, indicating that the reference count does not need to be increased and the ownership is not changed. The second is to increase the reference count value when converting the C pointer type. The third is to convert the Core Foundation pointer type to the obj-c pointer, and change the reference count value to + 1. For example, you can use the Core Foundation method to create an object and use arc to manage the object memory. Common Errors of arc transplantation 1. forced conversion of obj-c pointer bit C pointer (or reverse conversion) 2. in arc, the void * pointer is forcibly converted to the id type (or vice versa). To be converted, the modifier such as id selfPointer = (_ bridge void *) must be used *) self; 3. the object obj-c is used in the struct or (union) aggregate. 4. use NSAID utoreleasepool