Skip to content

ChainSwift πŸ”— is an extension that provides chainable way of setting properties in swift

License

Notifications You must be signed in to change notification settings

OrkhanAlikhanov/ChainSwift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

License

ChainSwift πŸ”—

ChainSwift is an extension that provides chainable way of setting properties in swift.

Why?

You can do this:

let titleLabel = UILabel().set
  .text("Welcome")
  .font(.systemFont(ofSize: 20))
  .textAlignment(.center)
  .get

Instead of this:

let titleLabel = UILabel()
titleLabel.text = "Welcome"
titleLabel.font = .systemFont(ofSize: 20)
titleLabel.textAlignment = .center

Usage

After installing add these lines to anywhere in your code:

import Foundation
import ChainSwift

extension NSObject: Chainable { } /// covers anything inheriting NSObject like UIView

Examples

Consider following class

class MyClass {
  var text = ""
  var int = 0
  var myEnum: MyEnum = .value1
}

After adding this single line:

extension MyClass: Chainable { }

You will be able to do:

let myClass = MyClass()

myClass.set
  .text("It works")
  .int(99)
  .myEnum(.value2)

print(myClass.text) // "It works"
print(myClass.int) // 99
print(myClass.myEnum) // .value2

/// immediate usage
let myClass = MyClass().set
  .text("It works")
  .int(99)
  .myEnum(.value2)
  .get // gains the instance back

How it works?

It combines Swift's Key Path Member Lookup and func callAsFunction features to achive this.

Other works

  • LayoutSwift - Yet another Swift Autolayout DSL for iOS.
  • SwiftUIRouter - An experimental navigation router for SwiftUI.

Installation

Swift Package Manager

Note: Instructions below are for using SwiftPM without the Xcode UI. It's the easiest to go to your Project Settings -> Swift Packages and add ChainSwift from there.

To integrate using Apple's Swift Package Manager , without Xcode integration, add the following as a dependency to your Package.swift:

dependencies: [
  .package(url: "https://github.com/OrkhanAlikhanov/ChainSwift.git", .upToNextMajor(from: "1.0.1"))
]

and then specify "ChainSwift" as a dependency of the Target in which you wish to use ChainSwift.

Manually

Just drag and drop the files in the Sources folder.

Authors

See also the list of contributors who participated in this project.

Love our work?

Hit the star 🌟 button! It helps! ❀️

License

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

About

ChainSwift πŸ”— is an extension that provides chainable way of setting properties in swift

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages