【发布时间】:2015-01-12 06:19:21
【问题描述】:
我是 iOS 8 的 Objective-C 新手,所以对 ARC 有点了解,我的代码在 ARC 下。
假设我有一个类 UserModel 具有 NSArray 和 NSString 等属性。我有 initWithDataSource:data 来分配初始化 UserModel。
从内存的角度来看,在块内设置属性是否安全?我觉得我的代码会导致任何保留周期。想知道我应该使用弱自我之类的东西还是其他东西来设置属性?
//在HomeViewController.m中
@interface HomeViewController() <UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UILabel *HomeLabel;
@property (strong, nonatomic) IBOutlet UICollectionView *ProjectCollectionView;
@property (strong, nonatomic) UserModel * HomeViewUserModel;
@end
/**
* fetch latest projects from remote side
*/
- (void) fetchUserModelFromRemote {
[MySharedInstance getProjectDataOnSuccess:^(id result) {
NSDictionary *data = result[@"data"];
self.HomeViewUserModel = [[UserModel alloc] initWithDataSource:data];
[[NSNotificationCenter defaultCenter] postNotificationName:@"alertCountUpdate" object:self userInfo:@{@"count": (NSNumber *)data[@"unread"]}];
}onFailure:^(id error) {}];
[MyCache cacheProjectListWithData:self.HomeViewUserModel];
}
【问题讨论】:
-
我觉得有一个retain循环,因为我在一个块内访问self,所以我应该使用weak self。是否有任何最佳实践来设置属性 insisde 块?
标签: ios objective-c