<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Swift ThenSwift 初始化語法糖

          聯(lián)合創(chuàng)作 · 2023-09-27 13:50

          Then 是實(shí)現(xiàn) Swift 對象初始化的一個語法糖

          初始化 UILabel 并設(shè)置其屬性

          let label = UILabel().then {
            $0.textAlignment = .center
            $0.textColor = .black
            $0.text = "Hello, World!"
          }

          相當(dāng)于:

          let label: UILabel = {
            let label = UILabel()
            label.textAlignment = .center
            label.textColor = .black
            label.text = "Hello, World!"
            return label
          }()

          技巧

          • 在所有的 NSObject 子類中使用 then() 

            let queue = OperationQueue().then {
              $0.maxConcurrentOperationCount = 1
            }
          • 在自定義類中使用,只需進(jìn)行簡單擴(kuò)展:

            extension MyType: Then {}
            
            let instance = MyType().then {
              $0.really = "awesome!"
            }
          • 拷貝值類型數(shù)據(jù)時候 with() 

            let newFrame = oldFrame.with {
              $0.size.width = 200
              $0.size.height = 100
            }
            newFrame.width // 200
            newFrame.height // 100
          • 使用 do() 減少類型處理

            UserDefaults.standard.do {
              $0.set("devxoul", forKey: "username")
              $0.set("[email protected]", forKey: "email")
              $0.synchronize()
            }

          真實(shí)示例:

          final class MyViewController: UIViewController {
          
            let titleLabel = UILabel().then {
              $0.textColor = .black
              $0.textAlignment = .center
            }
          
            let tableView = UITableView().then {
              $0.backgroundColor = .clear
              $0.separatorStyle = .none
              $0.register(MyCell.self, forCellReuseIdentifier: "myCell")
            }
          
            override func viewDidLoad() {
              super.viewDidLoad()
              self.view.addSubview(self.titleLabel)
              self.view.addSubview(self.tableView)
            }
          
          }

          安裝

          • For iOS 8+ projects with CocoaPods:

            pod 'Then'
          • For iOS 8+ projects with Carthage:

            github "devxoul/Then"
            
          • Using Swift Package Manager:

            import PackageDescription
            
            let package = Package(
              name: "MyAwesomeApp",
              dependencies: [
                .Package(url: "https://github.com/devxoul/Then", majorVersion: 2),
              ]
            )
          瀏覽 21
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  99久高清视频在线观看 | 国内视频精品在线 | 日本大色情www成人亚洲 | 日本操逼视频网站 | 人人操碰人人 |