iOS中的懶加載

iOS開發(fā)中 我們經(jīng)常使用懶加載
1.懶加載的好處,讓控件和對(duì)象在最需要加載的時(shí)候加載。這樣可以節(jié)省內(nèi)存空間,因?yàn)槲覀円苿?dòng)的設(shè)備資源還是比較寶貴的。所謂懶加載 就是推遲他的getter方法的執(zhí)行。
比如。一個(gè)view的子控件 ,只有當(dāng)這個(gè)view被顯示的時(shí)候才去加載。一個(gè)tableViewCell中,給他設(shè)置了圖片,他的content View里面才包含imageView的圖片,只有設(shè)置了textLabel的內(nèi)容,才會(huì)加載這個(gè)textLabel.

//collectionView的數(shù)據(jù)源--屬性申明
@property (nonatomic, strong) NSArray *famouseDoctorArr;

//懶加載
-(NSArray *)famouseDoctorArr{
if (_famouseDoctorArr == nil) {
_famouseDoctorArr =[FDFamousDoctorsTong famousDoctorsTongArr];
}
return _famouseDoctorArr;
}
// collectionView的懶加載屬性申明

@property (nonatomic, strong) UICollectionView *collectionView;

-(UICollectionView *)collectionView{
if (_collectionView == nil) {

    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZerocollectionViewLayout:self.layout];
    _collectionView.backgroundColor =[UIColor orangeColor];
    // 指定代理
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    //注冊(cè)cell
    //    [self.collectionView registerClass:[FDIllCollectionViewCell class] forCellWithReuseIdentifier:@"illCell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"FDIllCollectionViewCell"bundle:nil] forCellWithReuseIdentifier:@"illCell"];
    
}
return _collectionView;

}

-----說(shuō)說(shuō)我今天在懶加載中遇到的問題//1.0---我的illView中 collection View中的 cell被點(diǎn)擊了。

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//把模型傳遞給下一個(gè)控制器
FDFamousDoctorsTong *famouseDoctorTong = self.famouseDoctorArr[indexPath.item];

if (indexPath.item == self.famouseDoctorArr.count - 1) {
    NSLog(@"加載公益活動(dòng)界面");
}else{
    //跳轉(zhuǎn)到疾病選擇控制器 --通知主頁(yè)控制器切換
    if ([self.delegate respondsToSelector:@selector(illViewDidSelectItem:)]) {
        [self.delegate illViewDidSelectItem:famouseDoctorTong];
    }    
}

}

// 在獲取這個(gè)illView中控制器中實(shí)現(xiàn)代理方法

pragma mark - 疾病視圖cell被點(diǎn)擊的回調(diào)代理方法 ---執(zhí)行跳轉(zhuǎn)方法 傳遞模型

-(void)illViewDidSelectItem:(FDFamousDoctorsTong *)famouseDodctorTong{

// 從storyBoard里面加載Controller
UIStoryboard *sb =[UIStoryboard storyboardWithName:@"FDIllDetailViewController"bundle:nil];

//FDIllDetailViewController *detailVc =[sb instantiateViewControllerWithIdentifier:@"detailVC"];

FDIllDetailViewController *detailVc = sb.instantiateInitialViewController;
detailVc.title = @"疾病選擇詳情";
detailVc.famousDoctorsTong = famouseDodctorTong;
detailVc.view.backgroundColor =[UIColor colorWithRed:241/255.0 green:241/255.0blue:241/255.0 alpha:1.0];
[self.navigationController pushViewController:detailVc animated:YES];

}

//3.0重寫模型的setter方法給控件的屬性賦值

pragma mark - 從主頁(yè)控制器點(diǎn)擊疾病跳轉(zhuǎn)的得到參數(shù)重寫模型的setter方法

-(void)setFamousDoctorsTong:(FDFamousDoctorsTong *)famousDoctorsTong{
_famousDoctorsTong = famousDoctorsTong;
NSLog(@"famousDoctorsTong.title:%@",famousDoctorsTong.title);

self.illTypeLabel.text = [NSString stringWithFormat:@"疾病類型:%@",famousDoctorsTong.title];

}

好了看似沒有什么問題,但是打斷點(diǎn) 發(fā)現(xiàn) self.illTypeLabel 這個(gè)控件是nil
為什么 調(diào)用setter方法的時(shí)候還沒有加載這個(gè)控制器的view,所以子控件也是沒有加載的
----解決辦法 把賦值的代碼方法放到viewDidLoad里面去

  • (void)viewDidLoad {
    [super viewDidLoad];

    //TODO測(cè)試

    self.treatedMethodView.hidden = YES;

    [self.illDetailBtn setBackgroundColor:[UIColor clearColor]];
    [self.complicateBtn setBackgroundColor:[UIColor clearColor]];
    [self.selectTreatMethodBtn setBackgroundColor:[UIColor clearColor]];

      self.illTypeLabel.text =[NSString stringWithFormat:@"疾病類型:%@",self.famousDoctorsTong.title];
    

    }

// 最后來(lái)一個(gè)總結(jié)

懶加載的優(yōu)點(diǎn)不需將對(duì)象的實(shí)例化寫到viewDidLoad,可以簡(jiǎn)化代碼,增強(qiáng)代碼的可讀性
對(duì)象的實(shí)例化在getter方法中,各司其職,降低耦合性
對(duì)系統(tǒng)的內(nèi)存占用率會(huì)減小

viewDidLoad正常加載代碼示例
沒用懶加載的時(shí)候,從plist獲取數(shù)據(jù),返回一個(gè)數(shù)組,需要寫在viewDidLoad方法中獲取

@interface ViewController ()@property (nonatomic, strong) NSArray *shopData;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; _shopData = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"shop" ofType:@"plist"]];}@end

顯而易見,當(dāng)控制器被加載完成后就會(huì)加載當(dāng)前的shopData,假如shopData是在某些事件被觸發(fā)的時(shí)候才會(huì)被調(diào)用,沒必要在控制器加載完就去獲取plist文件,如果事件不被觸發(fā),代表著shopData永遠(yuǎn)不會(huì)被用到,這樣在viewDidLoad中加載shopData就會(huì)十分多余,并且耗用內(nèi)存

懶加載代碼示例

  • (void)viewDidLoad { [super viewDidLoad];}- (NSArray *)shopData{ if (!_shopData) { _shopData = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"shop" ofType:@"plist"]]; } return _shopData;}@end

當(dāng)需要用到shopData的時(shí)候,就會(huì)調(diào)用[self shopData]的方法(即getter方法),此時(shí)系統(tǒng)會(huì)去調(diào)用getter方法,然后再getter方法中獲取plist文件內(nèi)容,然后返回使用(需要注意在getter方法里切勿使用self.shopData,因?yàn)閟elf.shopData會(huì)調(diào)用getter方法,造成死循環(huán))總結(jié):懶加載即用到時(shí)方去加載對(duì)象

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容