ZTChain is a lightweight library designed to facilitate chaining syntax in Swift, making your code more readable and expressive.
This is currently the only solution that supports chained assignments for all Objective-C object properties.
- Allows for concise and readable code by chaining multiple method calls.
- Example:
view.zt.backgroundColor(.red).cornerRadius(10).borderWidth(2)
- Utilizes Swift’s
@dynamicMemberLookup
to provide a flexible and clean API. - Example:
view.zt.title("Welcome").subtitle("Hello World!")
- Uses Swift’s key paths to ensure type safety when accessing properties.
- Extends commonly used UIKit components like
UIView
,UILabel
,UIButton
, etc. - Example:
button.zt.title("Click Me").backgroundColor(.blue)
- Allows for easy creation of custom wrappers for any class or struct.
- Example:
MyCustomView().zt.customStyle
- Provides a fluent interface design pattern for improved code readability and maintainability.
- Example:
textField.zt.placeholder("Enter name").textColor(.gray)
- Easily extendable to support additional custom styles and properties.
- Example:
extension ZTWrapper where Subject: UIButton { ... }
- Compatible with iOS 11 and later versions.
- Integrates seamlessly with Xcode, ensuring build checks and validation.
- Provides full support for code suggestions and autocompletion in Xcode editor.
- The entire library is contained within a single file, making it easy to integrate and manage.
- Easily integrates with third-party libraries for extending UIKit properties.
To integrate using Apple's Swift Package Manager, add the following as a dependency to your Package.swift
:
dependencies: [
.package(url: "https://github.com/willonboy/ZTChain.git", .upToNextMajor(from: "2.0.0"))
]
import UIKit
import ZTChain
let tipsLbl = UILabel().zt
.frame(CGRect(x: 20, y: 200, width: 350, height: 100))
.textAlignment(.center)
.font(.boldSystemFont(ofSize: 18))
.textColor(.systemPink)
.numberOfLines(10)
.lineBreakMode(.byWordWrapping)
.backgroundColor(.systemGray)
.text("ZTChain is a lightweight library designed to facilitate chaining syntax in Swift, making your code more readable and expressive.")
.build()
// call function
self.window = UIWindow()
.zt
.frame(UIScreen.main.bounds)
.backgroundColor(.white)
.rootViewController(UINavigationController(rootViewController: ViewController()))
.call {
$0.makeKeyAndVisible()
}
.build()
This project is licensed under the AGPLv3.