Properties declaration for iOS: the difference between retain and strong

Source: Internet
Author: User

The strong or retain effect is the same when declaring attributes (it seems that more developers tend to use strong). However, when declaring a block, using strong and retain will have very different effects. Strong will be equal to copy, and retain is equal to assign!

Of course, the definition of block or should use copy (there are other areas to note, you can refer to this article: Ios:arc and non-arc using the Block property problem), because the non-arc under the copy block will be in the stack, arc block will be on the heap.

You can reproduce the problem this way. In a non-ARC environment, define a simple type, define a block property, and first use the correct copy:

@interfacevoid(^myblock) (); @end

Declare a variable in another type:

Testcls *_testobj

Then in a method, such as Viewdidload, set the block variable, note that even under non-arc, the block type that does not reference the external variable is also nsglobalblock, whereas a block referencing an external variable is Nsstackblock, the following code

_testobj = [[Testcls alloc] init]; int  A  = ^void() {    NSLog (@ "block called:%d", Outervar);}; NSLog (@ "block type:%@"class]);

Then execute the block in another method (such as UIButton's Click event method), as follows:

_testobj.myblock ();

The test environment is Xcode 6/ios 7/8, and if the top Myblock property declaration is copy or strong, the block will be copied and output:

block Type: __nsmallocblock__block is called:  A

If the above Myblock attribute declaration is retain or assign, the block behaves as assign, not copy, output

Block Type: __nsstackblock__

    

This may be crash (bad_access), or it might output the wrong value (which I will be testing under the console). Because the block scope is in the function stack, and the function is executed.

In short, strong and retain unexpectedly have a different place, and declare the Block property must be copy.

Related Posts:

IOS: Non-arc returns BLOCKIOS:ARC and non-arc using the Block property under the problem ios:nsdata/nsmutabledata binary data read and write online tool: From Objective-C. extracting function definitions in M files

Properties declaration for iOS: the difference between retain and strong

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.