Swift 5.懒加载

前端之家收集整理的这篇文章主要介绍了Swift 5.懒加载前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

ViewController.swift

import UIKit

class ViewController: UIViewController {

    
    
    //swift中 懒加载有非常特殊的写法  
    //实际上懒加载是 闭包
    lazy var nameLabel: UILabel = {
        print("我懒了么")
        let l = UILabel()
        l.textAlignment = NSTextAlignment.Center
        return l
    }()
    
    lazy var titleLabel: UILabel = UILabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        print(nameLabel)
//        print(nameLabel)
//        print(nameLabel)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
原文链接:https://www.f2er.com/swift/324963.html

猜你在找的Swift相关文章