<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"