Swiftility 0.13.0

Swiftility 0.13.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Sep 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Allan Bto.



Swiftility 0.13.0

  • By
  • Allan Bto

Swiftility

Framework grouping utilities to use in various projects

NB. This Framework is merely a collection of utilities and ‘nice to have’s.
Feel free to pick the methods and goodies that fit your project instead of including the pod.

Installation

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

pod "Swiftility"

Usage

import Swiftility

Features

MVVM

Checkout the MVVM implementation example I wrote using Swiftility

Core

Dynamic

Lightweight binding in Swift

struct Dynamic<T> {
    var value: T

    // ...

    init(_ value: T)

    // ...

    func bind(listener: T -> Void)

    // ...
}

/// Usage example

var email: Dynamic<String>("")

email.bind { value in
    print("Email is now: \(value)")
}

email.value = "[email protected]"
// prints "Email is now: [email protected]"

Grand Central Dispatch

Convenience use of GCD methods

async()

async {
    // Asynchronous code

    async_main {
        // Main thread code
    }
}

after()

after(2.5) {
    // Executed after 2.5 seconds
}

once()

once("someToken") {
    // Executed only once for the given token
}

debounce()

let debouncedFunction = debounced(0.5) { someVarOrVoid in
    // Executed only every 0.5 seconds
}

debouncedFunction(someVarOrVoid)

Type safety

Storyboard

/// Define Storyboard names

extension UIStoryboard.Name
{
    static let settings = UIStoryboard.Name(name: "Settings")
}

/// Define your view controller built in the `Settings` storyboard
// "SettingsViewController" should be the identifier in the storyboard (same as class name)

class SettingsViewController: UIViewController, FromStoryboard
{
    static let storyboardName: UIStoryboard.Name = .settings
}

/// Now you can do this

let settingsVC = SettingsViewController.instantiateFromStoryboard()

Nib

// "SettingsViewController.xib" should be the file containing SettingsViewController

class SettingsViewController: UIViewController, FromNib { ... }

// "MyView.xib" should be the file containing MyView

class MyView: UIView, FromNib { ... }

/// Now you can do this

let settingsVC = SettingsViewController.instantiateFromNib()

let myView = MyView.instantiateFromNib()

TableView

/// Define cell defined in "MyCell.xib"

class MyCell: UITableViewCell, FromNib { ... }

/// Register cell

tableView.register(MyCell.self)

/// Later

let cell = tableView.dequeueReusableCell() as MyCell

CollectionView

/// Define cell defined in "MyCell.xib"

class MyCell: UICollectionViewCell, FromNib { ... }

collectionView.register(MyCell.self)

/// Define custom view defined in "MyCustomView.xib"

class MyCustomView: UICollectionReusableView, FromNib { ... }

collectionView.register(MyCustomView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader)

/// Later

let supView: MyCustomView = collectioView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, for: someIndexPath)

let cell = collectioView.dequeueReusableCell(for: someIndexPath) as MyCell

Extensions

See Extensions page

Author

Allan Barbato, [email protected]

License

Swiftility is available under the MIT license. See the LICENSE file for more info.