项目作者: jormungand

项目描述 :
Configurable protocol: setup objects easy via closure
高级语言: Swift
项目地址: git://github.com/jormungand/Configurable.git
创建时间: 2018-12-11T17:44:17Z
项目社区:https://github.com/jormungand/Configurable

开源协议:MIT License

下载


Configurable

Configurable protocol: setup objects easy via closures

Usage

This tiny extension provides the approach to simplify and significantly increase visual perception of the code, to make it more structured, to segregate logic blocks and to get rid of local vars waste:

Before:

  1. override func loadView() {
  2. let view = UITextView(frame: .zero)
  3. view.backgroundColor = UIColor(white: 1.0, alpha: 0.1)
  4. view.isUserInteractionEnabled = false
  5. view.font = UIFont.monospacedDigitSystemFont(ofSize: 8.0, weight: UIFont.Weight.regular)
  6. view.layer.shadowColor = UIColor.white.cgColor
  7. view.layer.shadowRadius = 4.0
  8. view.layer.shadowOpacity = 0.9
  9. view.layer.shadowOffset = .zero
  10. view.layer.masksToBounds = false
  11. self.view = view
  12. }

After:

  1. override func loadView() {
  2. view = UITextView(frame: .zero).configure { view in
  3. view.backgroundColor = UIColor(white: 1.0, alpha: 0.1)
  4. view.isUserInteractionEnabled = false
  5. view.font = UIFont.monospacedDigitSystemFont(ofSize: 8.0, weight: UIFont.Weight.regular)
  6. view.layer.configure {
  7. $0.shadowColor = UIColor.white.cgColor
  8. $0.shadowRadius = 4.0
  9. $0.shadowOpacity = 0.9
  10. $0.shadowOffset = .zero
  11. $0.masksToBounds = false
  12. }
  13. }
  14. }

API

Configurable contains 2 protocols: Configurable and MutableConfigurable.

Both these protocols provide func configure(_:) which takes a configuration closure as a parameter. The difference between them is:

  • The Configurable protocol is dedicated for using with Swift and ObjC classes, as it passes Self into the configuration closure.
  • The MutableConfigurable protocol is dedicated for using with Swift Value Types (basic types, structs, enums, …), as it passes inout Self into the configuration closure.

Configurable – for Swift and ObjC classes

  1. let view = UIView(frame: .zero).configure { view in
  2. view.translatesAutoresizingMaskIntoConstraints = false
  3. view.backgroundColor = .green
  4. view.layer.configure {
  5. $0.cornerRadius = 5.0
  6. $0.masksToBounds = true
  7. }
  8. }

MutableConfigurable – for Swift Value Types (basic types, structs, enums, …)

  1. struct MyModelItem: MutableConfigurable {
  2. var itemID: Int = 0
  3. var name: String = ""
  4. // ....
  5. }
  6. let myItem = MyModelItem().configure {
  7. $0.itemID = 123
  8. $0.name = "name"
  9. // ....
  10. }

Requirements

  • Swift 4.2

" class="reference-link">Installation

CocoaPods

Configurable is available through CocoaPods. To install it, simply add the following line to your Podfile:

  1. pod "Configurable"

Carthage

To install Configurable add a dependency to your Cartfile:

  1. github "jormungand/Configurable"

Import

Import installed modules in your source files

  1. import Configurable

Author

Ilya Stroganov, ilya.stroganov@gmail.com

Linkedin: https://www.linkedin.com/in/ilyastroganov

License

This project is licensed under the MIT License - see the LICENSE file for details