From a weak reference to the crash talk weak assign strong application Scenario "iOS development tutorial"

Source: Internet
Author: User

<title>From a weak reference to the crash talk weak assign strong application scenario</title>

Method One of the definitions in. h:

@property(nonatomic,assign)NSArray*dataSource;

Define method Two

@property(nonatomic,strong)NSArray*dataSource;

How to implement in. m

- (void)Viewdidload {    [Super Viewdidload];    if (YES) {        Nsarray *array = @[@ "1", @ "2", @ "3"];        Self.datasource = Array;    }     if (Self.dataSource.count > 2) {        NSLog (@ "Self.datasource can I print?" Can print instructions can be defined as assign ");    }}

Results. H method? Crash, method? can be executed,

But what if it's a way to solve it?

- (void)Viewdidload {    [Super Viewdidload];    if (YES) {        Nsarray *array = @[@ "1", @ "2", @ "3"];        Self.datasource = [Array copy];    }     if (Self.dataSource.count > 2) {        NSLog (@ "Self.datasource can I print?" Can print instructions can be defined as assign ");    }}

Or

- (void)Viewdidload {    [Super Viewdidload];    if (YES) {        Nsarray *array = @[@ "1", @ "2", @ "3"];        Self.datasource = [Nsarray Arraywitharray:array];    }     if (Self.dataSource.count > 2) {        NSLog (@ "Self.datasource can I print?" Can print instructions can be defined as assign ");    }}

In short, these two improvements, and strong a truth, strong is in the setter, on the right side of the equal value of the strong operation, then why the above Self.datasource changed to _datasource also do not cause a crash? The _datasource compiler generated the setter method automatically and generated the _datasource Ivar variable, pointing to the same memory area.

At the same time, it can be seen that strong also affect the _datasource assignment. It's more intuitive to look at the MRC:

The setter of Arc

// _age的setter和getter- (void)setAge:(int)age{ _age = age;}

MRC Setter

//@property (nonatomic, retain) nsstring *name;- (void)SetName:(NSString *)name{    if (_name != name) {        [_name release];        _name = [Name retain];    }}
//@property (nonatomic, copy) NSString *name;- (void)SetName:(NSString *)name{    if (_name != name) {        [_name release];        _name = [Name copy];    }}

From a weak reference to the crash talk weak assign strong application Scenario "iOS development tutorial"

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.