iOS 属性关键字
2019-02-21 本文已影响49人
num_one






不同属性的Setter实现与Getter延长对象的生命周期
当属性为retain时,它的setter内部如何实现的:

当属性为copy时,它的setter内部如何实现:

Getter的方法能够延长对象的生命周期,它的内部代码如下:

小记:
assign VS weak
1.不改变修饰对象的引用计数。
2.assign修饰的对象被释放后,会产生悬垂指针,可能会crash。
3.weak修饰的对象被释放后,指针指向nil。
⚠️注意:delegate使用什么修饰符?
很早以前delegate使用的是assign(unsafe_unretained),后来apple官方文档里都变成了weak,当然偶尔会在apple官方文档中看到:
/* NSURLSession.h
Copyright (c) 2013-2018, Apple Inc. All rights reserved.
*/
@property (nullable, readonly, retain) id <NSURLSessionDelegate> delegate;
/* CoreAnimation - CAAnimation.h
Copyright (c) 2006-2018, Apple Inc.
All rights reserved. */
@property(nullable, strong) id <CAAnimationDelegate> delegate;
建议使用weak。
👏👏👏欢迎大家加入群组(IT_大前端技术交流群),技术交流群
