@interface ViewController ()@PRoperty (nonatomic,weak) id weakPoint;@property (nonatomic,assign) id assignPoint;@property (nonatomic,strong) id strongPoint;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.strongPoint = [NSDate date];NSLog(@"strong属性:%@",self.strongPoint);self.weakPoint = self.strongPoint;self.assignPoint = self.strongPoint;self.strongPoint = nil;NSLog(@"weak属性:%@",self.weakPoint);}@end当程序中的注释被打开时,运行程序有可能会崩溃(有时候不崩溃,你可能需要多运行几次),这是因为当assign指针所指向的内存被释放(释放并不等于抹除,只是引用计数为0),不会自动赋值nil,这样再引用self.assignPoint就会导致野指针操作,如果这个操作发生时内存还没有改变内容,依旧可以输出正确的结果,而如果发生时内存内容被改变了,就会crash。
结论:在ARC模式下编程时,指针变量一定要用weak修饰,只有基本数据类型和结构体需要用assgin,例如delegate,一定要用weak修饰。
新闻热点
疑难解答