Ios efficient development 2-what about ARC and block

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.