When to use lazy instantiation in iOS?

前端 未结 4 758
时光取名叫无心
时光取名叫无心 2020-12-12 23:05

I\'ve heard that lazy instantiation of objects in iOS is pretty common, however I\'m not exactly sure when I should use it? Could someone give a brief explanation of when I

4条回答
  •  时光取名叫无心
    2020-12-12 23:23

    Not only for memory and performance, check this out, here's another example:

    - (NSArray *)validElements{
        if (!_validElements) {
            _validElements = [[NSArray alloc] initWithObjects:
                              @"mystuff",@"generaldescription",@"title",@"autor",
                              @"version",@"date",@"context",@"operatingsystem",@"kindofdevice",
                              @"deviceversion",@"rule",@"daytime",@"time",@"location",@"deviceheading",
                              @"region",@"language",nil];
        }
        return _validElements;
    }
    

    You can use lazy instantiation for doing a custom init or special configuration and yes also this benefits memory and performance.

提交回复
热议问题