GithubHelp home page GithubHelp logo

orkhanalikhanov / chainswift Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 1.0 11 KB

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

License: MIT License

Swift 100.00%
swift chainable fluent

chainswift's Introduction

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

chainswift's People

Contributors

orkhanalikhanov avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

lylelh

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.