{"id":93,"date":"2025-06-04T10:20:11","date_gmt":"2025-06-05T03:13:10","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=93"},"modified":"2025-06-05T10:20:11","modified_gmt":"2025-06-05T03:13:10","slug":"swift-lazy-var-%e6%87%b6%e5%8a%a0%e8%bc%89-%e5%a4%a7%e8%a3%9c%e5%b8%96","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-lazy-var-%e6%87%b6%e5%8a%a0%e8%bc%89-%e5%a4%a7%e8%a3%9c%e5%b8%96\/","title":{"rendered":"2025 \u6700\u65b0 Swift \u61f6\u52a0\u8f09\uff08\u60f0\u6027\u8f09\u5165\uff09\u6280\u8853\u6559\u5b78"},"content":{"rendered":"<h1>2025 \u6700\u65b0 Swift \u61f6\u52a0\u8f09\uff08\u60f0\u6027\u8f09\u5165\uff09\u6280\u8853\u6559\u5b78<\/h1>\n<h2>\u4ec0\u9ebc\u662f\u61f6\u52a0\u8f09\uff08\u60f0\u6027\u8f09\u5165\uff09\uff1f<\/h2>\n<p>\u61f6\u52a0\u8f09\uff08Lazy Loading\uff09\u662f\u4e00\u7a2e\u8a2d\u8a08\u6a21\u5f0f\uff0c\u7576\u7528\u6236\u8a2a\u554f\u4e00\u500b\u9801\u9762\u6642\uff0c\u53ea\u6709\u5728\u9700\u8981\u986f\u793a\u7279\u5b9a\u5143\u4ef6\uff08\u5982 imageView\u3001Label \u7b49\uff09\u6642\uff0c\u624d\u6703\u9032\u884c\u52a0\u8f09\u3002\u9019\u6a23\u53ef\u4ee5\u6e1b\u5c11\u521d\u59cb\u52a0\u8f09\u7684\u6642\u9593\uff0c\u5f9e\u800c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002\u61f6\u52a0\u8f09\u7279\u5225\u9069\u5408\u65bc\u5167\u5bb9\u8c50\u5bcc\u3001\u9801\u9762\u9577\u4e14\u8907\u96dc\u7684\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002\u900f\u904e\u9019\u7a2e\u65b9\u5f0f\uff0c\u53ea\u5728\u9700\u8981\u6642\u52a0\u8f09\u8cc7\u6599\uff0c\u907f\u514d\u4e86\u9810\u5148\u52a0\u8f09\u53ef\u80fd\u5c0e\u81f4\u7684\u6027\u80fd\u74f6\u9838\u3002<\/p>\n<h2>\u70ba\u4ec0\u9ebc\u8981\u4f7f\u7528\u61f6\u52a0\u8f09\uff1f<\/h2>\n<p>\u4f7f\u7528\u61f6\u52a0\u8f09\u7684\u4e3b\u8981\u539f\u56e0\u5305\u62ec\uff1a<br \/>\n&#8211; **\u63d0\u5347\u6027\u80fd**\uff1a\u61f6\u52a0\u8f09\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7684\u555f\u52d5\u901f\u5ea6\uff0c\u56e0\u70ba\u4e0d\u9700\u8981\u4e00\u6b21\u6027\u52a0\u8f09\u6240\u6709\u5143\u4ef6\u3002<br \/>\n&#8211; **\u7bc0\u7701\u8cc7\u6e90**\uff1a\u901a\u904e\u6e1b\u5c11\u4e0d\u5fc5\u8981\u7684\u8cc7\u6e90\u4f7f\u7528\uff0c\u964d\u4f4e\u4e86\u61c9\u7528\u7684\u5167\u5b58\u5360\u7528\uff0c\u7279\u5225\u5728\u79fb\u52d5\u8a2d\u5099\u4e0a\u5c24\u70ba\u91cd\u8981\u3002<br \/>\n&#8211; **\u6539\u5584\u7528\u6236\u9ad4\u9a57**\uff1a\u7528\u6236\u5728\u4f7f\u7528\u61c9\u7528\u6642\uff0c\u80fd\u5920\u66f4\u5feb\u5730\u7372\u5f97\u6240\u9700\u5167\u5bb9\uff0c\u5f9e\u800c\u63d0\u5347\u6574\u9ad4\u9ad4\u9a57\u3002<\/p>\n<h2>Swift \u61f6\u52a0\u8f09\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f Swift \u4e2d\u4f7f\u7528\u61f6\u52a0\u8f09\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u4e0d\u540c\u5143\u4ef6\u4e2d\u5be6\u73fe\u61f6\u52a0\u8f09\u3002<\/p>\n<h3>UIView<\/h3>\n<p>&#8220;`swift<br \/>\nlazy var topView: UIView = {<br \/>\n    let topView = UIView()<br \/>\n    topView.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: 48 + kSafeTopPadding)<br \/>\n    topView.backgroundColor = UIColor(red: 46.0\/255.0, green: 144.0\/255.0, blue: 133.0\/255.0, alpha: 1.0)<br \/>\n    return topView<br \/>\n}()<br \/>\n&#8220;`<\/p>\n<h3>UILabel<\/h3>\n<p>&#8220;`swift<br \/>\nlazy var titleLabel: UILabel = {<br \/>\n    let titleLabel = UILabel()<br \/>\n    titleLabel.frame = CGRect(x: topView.center.x &#8211; kScreenWidth * 0.66 \/ 2, y: kSafeTopPadding, width: self.frame.width * 0.66, height: self.topView.frame.height &#8211; kSafeTopPadding)<br \/>\n    titleLabel.text = &#8220;\u6211\u7684\u9080\u8acb\u78bc&#8221;<br \/>\n    titleLabel.textColor = .white<br \/>\n    titleLabel.textAlignment = .center<br \/>\n    titleLabel.font = UIFont(name:&#8221;PingFangTC-Semibold&#8221;, size: 20.0)<br \/>\n    return titleLabel<br \/>\n}()<br \/>\n&#8220;`<\/p>\n<h3>UIButton<\/h3>\n<p>&#8220;`swift<br \/>\nlazy var friendInviteButton: UIButton = {<br \/>\n    let friendInviteButton = UIButton()<br \/>\n    friendInviteButton.setTitle(&#8220;\u597d\u53cb\u9080\u8acb\u7d00\u9304&#8221;, for: .normal)<br \/>\n    friendInviteButton.titleLabel?.font = UIFont(name: &#8220;PingFangTC-Semibold&#8221;, size: 16)<br \/>\n    friendInviteButton.setTitleColor(UIColor(red: 128\/255, green: 128\/255, blue: 128\/255, alpha: 1), for: .normal)<br \/>\n    friendInviteButton.setTitleColor(UIColor(red: 71\/255, green: 171\/255, blue: 161\/255, alpha: 1), for: .selected)<br \/>\n    friendInviteButton.backgroundColor = UIColor.white<br \/>\n    friendInviteButton.addTarget(self, action: #selector(friendInviteButtonPress(sender:)), for: .touchUpInside)<br \/>\n    friendInviteButton.setImage(UIImage(named: &#8220;notice&#8221;), for: .normal)<br \/>\n    friendInviteButton.imageView?.contentMode = .scaleAspectFit<br \/>\n    return friendInviteButton<br \/>\n}()<br \/>\n&#8220;`<\/p>\n<h3>UICollectionView<\/h3>\n<p>&#8220;`swift<br \/>\nlazy var sliderCollectionView: UICollectionView = {<br \/>\n    let layout = UICollectionViewFlowLayout()<br \/>\n    layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)<br \/>\n    layout.minimumLineSpacing = 5<br \/>\n    layout.scrollDirection = .horizontal<br \/>\n    let sliderCollectionView = UICollectionView(frame: CGRect(x: kRealValue(value: 17), y: topView.frame.maxY + kRealHeightValue(value: 10), width: kScreenWidth &#8211; kRealValue(value: 17) * 2, height: (kScreenWidth &#8211; kRealValue(value: 17) * 2) * 480\/1080), collectionViewLayout: layout)<br \/>\n    sliderCollectionView.backgroundColor = UIColor.white<br \/>\n    sliderCollectionView.layer.cornerRadius = 10<br \/>\n    sliderCollectionView.register(UINib(nibName:&#8221;BannerImageCell&#8221;, bundle:nil), forCellWithReuseIdentifier:&#8221;BannerImageCell&#8221;)<br \/>\n    sliderCollectionView.delegate = self<br \/>\n    sliderCollectionView.dataSource = self<br \/>\n    sliderCollectionView.isPagingEnabled = true<br \/>\n    return sliderCollectionView<br \/>\n}()<br \/>\n&#8220;`<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528\u61f6\u52a0\u8f09\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u554f\u984c\uff1a<br \/>\n&#8211; **\u5143\u4ef6\u672a\u986f\u793a**\uff1a\u78ba\u4fdd\u61f6\u52a0\u8f09\u7684\u8b8a\u6578\u5df2\u88ab\u6b63\u78ba\u521d\u59cb\u5316\uff0c\u4e26\u5728\u9700\u8981\u6642\u88ab\u6b63\u78ba\u5f15\u7528\u3002<br \/>\n&#8211; **\u6027\u80fd\u554f\u984c**\uff1a\u5982\u679c\u61f6\u52a0\u8f09\u7684\u5143\u4ef6\u5728\u521d\u6b21\u986f\u793a\u6642\u4ecd\u7136\u611f\u89ba\u5361\u9813\uff0c\u8003\u616e\u9032\u4e00\u6b65\u512a\u5316\u5143\u4ef6\u7684\u8a2d\u8a08\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u61f6\u52a0\u8f09\u6280\u8853\u4e0d\u50c5\u53ef\u4ee5\u61c9\u7528\u65bc\u8996\u5716\u5143\u4ef6\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u6578\u64da\u52a0\u8f09\u3002\u4f8b\u5982\uff0c\u5728 UITableView \u6216 UICollectionView \u4e2d\uff0c\u50c5\u5728\u7528\u6236\u6efe\u52d5\u5230\u7279\u5b9a\u4f4d\u7f6e\u6642\u624d\u52a0\u8f09\u66f4\u591a\u6578\u64da\uff0c\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f09\u5927\u91cf\u6578\u64da\u9020\u6210\u7684\u6027\u80fd\u554f\u984c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png\" alt=\"Swift\u61f6\u52a0\u8f09\" title=\"\"><\/p>\n<p>\u9019\u5f35\u5716\u7247\u5c55\u793a\u4e86 Swift \u61f6\u52a0\u8f09\u7684\u6982\u5ff5\uff0c\u5e6b\u52a9\u7406\u89e3\u60f0\u6027\u8f09\u5165\u5728\u5be6\u969b\u61c9\u7528\u4e2d\u7684\u91cd\u8981\u6027\u3002<\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff08Q&#038;A\uff09<\/h2>\n<h3>Q1: \u61f6\u52a0\u8f09\u6703\u5f71\u97ff\u61c9\u7528\u7684\u6027\u80fd\u55ce\uff1f<\/h3>\n<p>A: \u6b63\u78ba\u4f7f\u7528\u61f6\u52a0\u8f09\u53ef\u4ee5\u63d0\u5347\u61c9\u7528\u6027\u80fd\uff0c\u6e1b\u5c11\u521d\u59cb\u52a0\u8f09\u6642\u9593\u3002\u82e5\u4f7f\u7528\u4e0d\u7576\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4\u52a0\u8f09\u5ef6\u9072\uff0c\u5f71\u97ff\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u5224\u65b7\u4f55\u6642\u4f7f\u7528\u61f6\u52a0\u8f09\uff1f<\/h3>\n<p>A: \u7576\u4f60\u7684\u61c9\u7528\u5305\u542b\u5927\u91cf\u6578\u64da\u6216\u5716\u7247\uff0c\u4e14\u9019\u4e9b\u6578\u64da\u4e0d\u5fc5\u5728\u521d\u59cb\u52a0\u8f09\u6642\u7acb\u5373\u986f\u793a\u6642\uff0c\u5247\u4f7f\u7528\u61f6\u52a0\u8f09\u662f\u500b\u4e0d\u932f\u7684\u9078\u64c7\u3002<\/p>\n<h3>Q3: \u61f6\u52a0\u8f09\u5728 Swift \u4e2d\u7684\u6700\u4f73\u5be6\u8e10\u662f\u4ec0\u9ebc\uff1f<\/h3>\n<p>A: \u4f7f\u7528 lazy \u95dc\u9375\u5b57\u4f86\u5ef6\u9072\u8b8a\u6578\u7684\u52a0\u8f09\uff0c\u4e26\u78ba\u4fdd\u5728\u9700\u8981\u6642\u624d\u9032\u884c\u5be6\u969b\u7684\u521d\u59cb\u5316\uff0c\u9019\u6a23\u53ef\u4ee5\u6709\u6548\u63d0\u5347\u61c9\u7528\u6027\u80fd\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u4ee5\u4e0a\u5167\u5bb9\u5df2\u6839\u64da\u60a8\u7684\u8981\u6c42\u9032\u884c\u512a\u5316\u3001\u66f4\u65b0\uff0c\u4e26\u7b26\u5408 Google SEO \u6700\u4f73\u5be6\u8e10\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>### UIView<br \/>\nlazy var topView: UIView = {<br \/>\n    let topView = UIView()<br \/>\n    topView.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: 48 + kSafeTopPadding)<br \/>\n    topView.backgroundColor = UIColor.init(red: 46.0\/255.0, green: 144.0\/255.0, blue: 133.0\/255.0, alpha: 1.0)<br \/>\n    return topView<br \/>\n}()<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-93","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1v","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":13,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"predecessor-version":[{"id":7587,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/93\/revisions\/7587"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}