美文网首页
Swift 重构:点语法声明总结

Swift 重构:点语法声明总结

作者: SoaringHeart | 来源:发表于2021-06-16 14:56 被阅读0次

一、常规声明

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

二、特殊声明

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

三、通过第三方库 then

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

四、通过 ReferenceWritableKeyPath

protocol After {}

extension After where Self: AnyObject {
    @discardableResult
    func after<T>(_ property: ReferenceWritableKeyPath<Self, T>, setTo value: T) -> Self {
        self[keyPath: property] = value
        return self
    }
}

extension UIView: After {}
//demo
func test(){
    UILabel()
        .after(\.textAlignment, setTo: .center)
        .after(\.backgroundColor, setTo: .red)
 }

第一种自不必多说,第二种和第三种方式可以让代码的更加的优雅;

如果要实现如下设置效果:

WechatIMG19.jpeg
    let barTintColor: UIColor = UIColor.systemBlue
    let tintColor = UIColor.white;

方式 1 实现:

    let navigationBar = UINavigationBar.appearance();
    navigationBar.barTintColor = barTintColor;
    navigationBar.tintColor = tintColor;
    navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: tintColor,]

    
    let segmentedControl = UISegmentedControl.appearance(whenContainedInInstancesOf: [UINavigationBar.self])
    segmentedControl.tintColor = tintColor
    segmentedControl.selectedSegmentIndex = 0
    segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: tintColor,
    ], for: .normal)
      segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: barTintColor,
    ], for: .selected)

    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UIImagePickerController.self])
        .setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)

方式 2 实现:

    _ = {
        $0.barTintColor = barTintColor
        $0.tintColor = tintColor
        $0.titleTextAttributes = [NSAttributedString.Key.foregroundColor: tintColor,]
      }(UINavigationBar.appearance())
    
    _ = {
        $0.tintColor = tintColor
        $0.selectedSegmentIndex = 0

        $0.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: tintColor,
        ], for: .normal)
        $0.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: barTintColor,
        ], for: .selected)
      }(UISegmentedControl.appearance(whenContainedInInstancesOf: [UINavigationBar.self]))
    
    _ = {
        $0.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)
      }(UIBarButtonItem.appearance(whenContainedInInstancesOf: [UIImagePickerController.self]))

方式 3 实现:

    UINavigationBar.appearance().then {
        $0.barTintColor = barTintColor
        $0.tintColor = tintColor
        $0.titleTextAttributes = [NSAttributedString.Key.foregroundColor: tintColor,]
    }

    UISegmentedControl.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).then {
        $0.tintColor = tintColor
        $0.selectedSegmentIndex = 0

        $0.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: tintColor,
        ], for: .normal)
        $0.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: barTintColor,
        ], for: .selected)
    }

    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UIImagePickerController.self]).then {
        $0.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)
    }

结论:方式 2 和 3, 在特殊情况下比方式 1 更加优雅;方式 3 需要导入第三方库实现,方式 2 可以作为 3 的补充,在不方便导入第三库的代码里使用。

相关文章

  • Swift 重构:点语法声明总结

    一、常规声明 二、特殊声明 三、通过第三方库 then 四、通过 ReferenceWritableKeyPath...

  • Swift最新语法总结

    swift最新语法总结(函数) swift最新语法总结(枚举类型与结构体) swift最新语法总结(类的使用) s...

  • Swift 中为什么要有 willSet 和 didSet,它们

    Swift 拥有类似 C# 的属性声明语法: 但是,Swift 还另有 willSet 和 didSet 行为。它...

  • swift字符串

    Swift字符串常用操作总结 版本2:增加了Swift 2.0的语法,与Swift 1.2的语法相比,主要是:ad...

  • Swift 枚举(enum)详解

    Swift基础语法总结,来自苹果官方文档: 枚举(Enumeration) 枚举在Swift中是 first-cl...

  • Swift总结

    swift总结(整理) -- 基本语法 Import swift的import等同于C/C++的include,不...

  • Swift 语法浅聊

    Swift 语法浅聊 1. 简单值 使用** let 来声明常量,使用 var **来声明变量. 简单值数据类型要...

  • swift语法总结

    swift语法 swift初见第一个程序输出hello,world! 在swift中这行代码就算一个完整的程序。你...

  • Swift 4.0 基础学习总结(二)

    在 Swift 4.0 基础学习总结(一)中,我们学习的Swift中的基本数据类型以及控制流,Swift简介的语法...

  • Swift 基本语法04-"switch"和

    Swift 基本语法01-Swift简介Swift 基本语法02-"let"和"var"Swift 基本语法03-...

网友评论

      本文标题:Swift 重构:点语法声明总结

      本文链接:https://www.haomeiwen.com/subject/jpoxyltx.html