【案例】:
@interface FTAppCenterMainViewController (){}@property(weak,nonatomic) UITableView* myTableView;@end @property (nonatomic, weak) id<ftactionsheetdelegate>delegate;</ftactionsheetdelegate>
【推荐方法】:
child只有parent的对象为weak类型:
@property (nonatomic, weak) id<ftactionsheetdelegate>delegate;</ftactionsheetdelegate>
【案例】:
看下面的代码:
typedef void (^RequestNaviCallBack)(NSInteger naviCode,NSInteger httpCode,NSError * error);@interface FtNaviManager : NSObject{}@property (nonatomic, strong) RequestNaviCallBack naviCallBack;
【推荐方法】:
如果有循环引用,编译器会提示警告。
如果对象没有持有Block对象,那么不会产生循环引用。如果对象持有了block对象,那么在block引用self的时候这么定义:
__weak typeof(self) weakSelf = self;
【案例】:
@interface FtKeepAlive : NSObject{ NSTimer* _keepAliveTimer; // 发送心跳timer}//实现文件_keepAliveTimer = [NSTimer scheduledTimerWithTimeInterval:_expired target:self selector:@selector(keepLiveStart) userInfo:nil repeats:YES];
类持有了_keepAliveTimer,_keepAliveTimer又持有了self,造成循环引用。
【推荐方法】:
NSTimer会持有对象,所以:在删除对象之前,需要将timer的invalidate方法。
-(void)stopKeepAlive{ [_keepAliveTimer invalidate]; _keepAliveTimer = nil;}原文:http://www.cnblogs.com/niexiaobo/p/4932609.html