Why do block modifiers use copy and block modifier copy?

Source: Internet
Author: User
Tags modifiers

Why do block modifiers use copy and block modifier copy?

Presumably many developers know that the block is modified by copy. Next we will explain why the block needs to be modified by copy. We will even talk about how to use strong to modify the block.

In Objective-C, there are three types of blocks:

Create a new project and test it in the ARC environment and MRC environment respectively.

To change the test file to the MRC environment:

The complete test code is shown as follows:

# Import "ViewController. h"

@ Interface ViewController ()

@ Property (nonatomic, copy) void (^ demoBolck )();

@ Property (nonatomic, strong) void (^ demoBolck1 )();

@ End

@ Implementation ViewController

Int B = 8; // global variable

-(Void) viewDidLoad {

[Super viewDidLoad];

Void (^ demoBolck) () = ^ {

NSLog (@ "indemoBolck ");

};

NSLog (@ "demoBolck % @", demoBolck); // <__ NSGlobalBlock __: 0x1085af0e0> no matter under ARC or MRC, because external regions are not accessed (including those without external variables or only global variables), NSGlobalBlock indicates that

Void (^ demoBolck4) () = ^ {

NSLog (@ "indemoBolck4% d", B );

};

NSLog (@ "demoBolck4% @", demoBolck4); // <__ NSGlobalBlock __: 0x10150b120> global Zone

_ Block int a = 6; // block references a internally and modifies its value. You need to modify the value using block. Otherwise, you do not need to use block.

Void (^ demoBolck2) () = ^ {

NSLog (@ "indemoBolck2% d", a ++ );

};

DemoBolck2 ();

NSLog (@ "demoBolck2% @, % d", demoBolck2, a); // <__ NSMallocBlock __: 0x60042556c50> in ARC mode, the system also performs copy on the Block by default. The Block memory address is displayed in the stack zone <__ NSStackBlock __: 0x7fff5d0ada28> MRC in the stack zone.

NSLog (@ "demoBolck2.Copy % @", [demoBolck2 copy]); // <__ NSMallocBlock __: 0x60042556c50> the copy operation is in the heap zone regardless of whether MRC or ARC, only copy in MRC will change the address

Self. demoBolck = demoBolck2;

NSLog (@ "self. demoBolck % @", self. demoBolck); // heap <__ NSMallocBlock __: 0x608000052630>

Self. demoBolck1 = demoBolck2;

Self. demoBolck1 (); // you can run demoBolck2 7 without any problems.

NSLog (@ "self. demoBolck1 % @ ", self. demoBolck1); // <__ NSMallocBlock __: 0x60042556c50> it is no problem to modify ARC and MRC by strong, but assign and retain are still in the stack zone in the MRC environment and there will be problems.

}

-(Void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {

// Note: In the MRC environment, demoBolck1 is modified with assign, retain, and stack zone: <__ NSStackBlock __: 0x7fff50915a50>. If it is released in advance, the program running in the following statements will crash. In the ARC environment: You can print the results normally by using copy, strong, assign, and retain. Regardless of the environment, the copy and strong modifier can print the results normally.

Self. demoBolck1 ();

}

@ End

Summary:

  • When no external local variables are called inside the block, they are stored in the global zone (both under ARC and MRC)
  • Block uses external local variables, which is also a common method. MRC: the memory address of the Block is displayed in the stack area. The stack area features that the created object may be destroyed at any time. Once destroyed, the program may crash when empty objects are called again, after the block is copied, the block is stored in the heap. therefore, use copy to modify the Block attribute. However, all blocks in ARC are stacked, and the system will copy the blocks by default.
  • Using copy and strong to modify the block can be used in both ARC and MRC, both in the heap zone.

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.