- OC版
-
// classA.h@property (nonatomic, copy) NSString *testString;//classA.m- (NSString*)testString { if (!_testString) { _testString = @"Hello"; NSLog(@"Print at first time"); } return _testString}
- Swift版
-
//在变量属性前加 lazy 指定延时加载class ClassA { lazy var str: String = { let str = "Hello" print("print at first time") return str }() // or // lazy var str: String = "Hello"}// lazy + array.map / filterlet data = 1...3let result = data.lazy.map { (i: Int) -> Int in print("i is \(i)") return i * 2}print("print results")for i in result { print("result is \(i)")}print("end")/*print resultsi is 1result is 2i is 2result is 4i is 3result is 6end*/