在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1.常见懒加载方法 class ViewController: UIViewController { lazy var label: UILabel = { let label = UILabel(frame: .zero) label.translatesAutoresizingMaskIntoConstraints = false label.textColor = .black label.font = UIFont.systemFont(ofSize: 16, weight: .bold) return label }() }
2.懒加载使用weak 还是 unowned? [weak self] in 还是用 [unowned self] in
3.工厂方法 + 懒加载 是
class ViewController: UIViewController { lazy var label: UILabel = self.createCustomLabel() private func createCustomLabel() -> UILabel { print("被调用") let label = UILabel(frame: .zero) label.translatesAutoresizingMaskIntoConstraints = false label.textColor = .black label.font = UIFont.systemFont(ofSize: 16, weight: .bold) return label } }
4.如果希望在多处使用,使用静态工厂 + 懒加载 class ViewController: UIViewController { lazy var label: UILabel = UILabel.createCustomLabel() } extension UILabel { static func createCustomLabel() -> UILabel { let label = UILabel(frame: .zero) label.translatesAutoresizingMaskIntoConstraints = false label.textColor = .black label.font = UIFont.systemFont(ofSize: 16, weight: .bold) return label } }
|
请发表评论