All Projects → ytyubox → SwiftBuilder

ytyubox / SwiftBuilder

Licence: other
SwiftBuilder is a fast way to assign new value to the property of the object.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to SwiftBuilder

Human Interface Guidelines Extras
Community additions to Apple's Human Interface Guidelines
Stars: ✭ 225 (+765.38%)
Mutual labels:  tvos, watchos
BalloonPopup
Forget Android Toast! BalloonPopup displays a round or squared popup and attaches it to a View, like a callout. Uses the Builder pattern for maximum ease. The popup can automatically hide and can persist when the value is updated.
Stars: ✭ 32 (+23.08%)
Mutual labels:  builder, builder-pattern
Wwdc
You don't have the time to watch all the WWDC session videos yourself? No problem me and many contributors extracted the gist for you 🥳
Stars: ✭ 2,561 (+9750%)
Mutual labels:  tvos, watchos
Outlaw
JSON mapper for macOS, iOS, tvOS, and watchOS
Stars: ✭ 24 (-7.69%)
Mutual labels:  tvos, watchos
Orchard
Device identification in Swift and Objective-C for iOS, watchOS, and tvOS.
Stars: ✭ 15 (-42.31%)
Mutual labels:  tvos, watchos
Cache
Swift caching library
Stars: ✭ 210 (+707.69%)
Mutual labels:  tvos, watchos
Swiftui Sliders
🚀 SwiftUI Sliders with custom styles
Stars: ✭ 241 (+826.92%)
Mutual labels:  tvos, watchos
Wwdc Notes
WWDCNotes.com content ✨
Stars: ✭ 183 (+603.85%)
Mutual labels:  tvos, watchos
WWDCNotes
WWDCNotes.com content
Stars: ✭ 343 (+1219.23%)
Mutual labels:  tvos, watchos
ScaledFont
ScaledFont - Using custom fonts with dynamic type
Stars: ✭ 50 (+92.31%)
Mutual labels:  tvos, watchos
Htmlkit
An Objective-C framework for your everyday HTML needs.
Stars: ✭ 206 (+692.31%)
Mutual labels:  tvos, watchos
RFKit
Toolkit for daily Cocoa development. Since 2012.
Stars: ✭ 20 (-23.08%)
Mutual labels:  tvos, watchos
Ios Crash Dump Analysis Book
iOS Crash Dump Analysis Book
Stars: ✭ 158 (+507.69%)
Mutual labels:  tvos, watchos
Iso8601
ISO8601 date parser and writer
Stars: ✭ 213 (+719.23%)
Mutual labels:  tvos, watchos
Objc Sdk
LeanCloud Objective-C SDK
Stars: ✭ 186 (+615.38%)
Mutual labels:  tvos, watchos
Fire
🔥A delightful HTTP/HTTPS networking framework for iOS/macOS/watchOS/tvOS platforms written in Swift.
Stars: ✭ 243 (+834.62%)
Mutual labels:  tvos, watchos
Anydate
Swifty Date & Time API inspired from Java 8 DateTime API.
Stars: ✭ 178 (+584.62%)
Mutual labels:  tvos, watchos
L10n Swift
Localization of the application with ability to change language "on the fly" and support for plural form in any language.
Stars: ✭ 177 (+580.77%)
Mutual labels:  tvos, watchos
Futures
Lightweight promises for iOS, macOS, tvOS, watchOS, and Linux
Stars: ✭ 59 (+126.92%)
Mutual labels:  tvos, watchos
wwdc2018
You read my developer triceraptus migration notes from dub dub dc 2018
Stars: ✭ 48 (+84.62%)
Mutual labels:  tvos, watchos

SwiftBuilder

Swift Builder is a fast way to assign new value to the property of the object. Thanks to Hsu Li-Heng and his great article 利用 Swift 5.1 新功能實作 Fluent Interface 讓程式碼更易讀流暢!.

Swift bulid codecov Platform Swift Xcode SPM MIT

Key Concept of Fluent Interface

A fluent interface is a method for designing object-oriented APIs that relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language. It was coined in 2005 by Eric Evans and Martin Fowler.

Designing a Fluent Interface

A fluent interface is implemented by using method chaining to implement method cascading, concretely by having each method return self. Stated more abstractly, a fluent interface relays the instruction context of a subsequent call in method chaining, where generally the context is

  1. Defined through the return value of a called method
  2. Self-referential, where the new context is equivalent to the last context
  3. Terminated through the return of a void context

Note that a "fluent interface" means more than just method cascading via chaining; it entails designing an interface that reads like a Domain-specific language(DSL), using other techniques like "nested functions and object scoping"

Before swift 5.1

In Swift 3.0+, returning self in the functions is one way to implement the fluent pattern.

class Person {
    var firstname: String = ""
    var lastname: String = ""
    var favoriteQuote: String = ""

    @discardableResult
    func set(firstname: String) -> Self {
        self.firstname = firstname
        return self
    }

    @discardableResult
    func set(lastname: String) -> Self {
        self.lastname = lastname
        return self
    }

    @discardableResult
    func set(favoriteQuote: String) -> Self {
        self.favoriteQuote = favoriteQuote
        return self
    }
}


let person = Person()
    .set(firstname: "John")
    .set(lastname: "Doe")
    .set(favoriteQuote: "I like turtles")

After Swift 5.1

By the power of Dynamic Member Lookup in Swift 4.2, and it's followed up evolve feature, Key Path Member Lookup in Swift 5.1 we can acheive Fluent Interface, by the article of Hsu Li-Heng: [利用 Swift 5.1 新功能實作 Fluent Interface 讓程式碼更易讀流暢!]. I Highly recommend reading through.

How to use

// SwiftBuilderExport.swift
 @_exported import struct SwiftBuilder.Builder 
struct Point {
    var x:Int
    var y:Int
    init(x:Int = 0, y:Int = 0) {
        self.x = x
        self.y = y
    }
}

let point = Builder(Point())
              .x(1)
              .y(2)
              .build()
// point now have x:1 and y: 2

Requirements

  • Swift 5.1

Installatio Using Swift Package Manager

The Swift Package Manager is a decentralized dependency manager for Swift.

  1. Add the project to your Package.swift.

    import PackageDescription
    
    let package = Package(
        ...
        dependencies: [
          ...
            .Package(url: "https://github.com/ytyubox/SwiftBuilder", from: "2.0.0"),
        ],
        targets: [
        .target(
            name: "...",
            dependencies: ["SwiftBuilder"])
    )

Using your operator

We can using Customize by adopt the prefix and postfix operator as follow:

import SwiftBuilder

postfix operator >|
public postfix func >| <T>(lhs: T) -> FluentInterface<T> {
  return Builder(lhs)
}

postfix operator |>

public postfix func |> <T>(lhs: FluentInterface<T>) -> T {
    return lhs.build()
}

Contribute

Working on your first Pull Request? You can learn how from this free series, How to Contribute to an Open Source Project on GitHub.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].