Latest 0.9.2
Homepage https://github.com/allbto/ios-swiftility
License MIT
Platforms ios 8.0, requires ARC

CocoaPods Status
Build Status
License
Platform
[Swift-2.2]()

Framework grouping my utilities to use in various projects

Installation

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

pod "Swiftility"

Usage

import Swiftility

MVVM

Checkout the MVVM implementation example I wrote using Swiftility

Type safe UIKit

Storyboard

/// Define Storyboard convertible type

enum Storyboards: String, StoryboardConvertible {
    case Main, Settings, Explore

    var storyboardName: String {
        return self.rawValue
    }
}

/// 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 ownStoryboard: StoryboardConvertible = Storyboards.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.registerCell(MyCell)

/// Later

let cell: MyCell = tableView.dequeueReusableCell(MyCell)

CollectionView

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

class MyCell: UICollectionViewCell, FromNib { ... }

collectionView.registerCell(MyCell)

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

class MyCustomView: UICollectionReusableView, FromNib { ... }

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

/// Later

let supView: MyCustomView = collectioView.dequeueReusableSupplementaryView(kind: UICollectionElementKindSectionHeader, forIndexPath: someIndexPath)

let cell: MyCell = collectioView.dequeueReusableCell(MyCell.self, forIndexPath: someIndexPath)

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 queues

enum GCDQueue {
    case Main
    case High
    case Default
    case Low
    case Background
    case Custom(dispatch_queue_t)
}

async()

func async(queue: GCDQueue = .Default, closure: dispatch_block_t)
func async_main(closure: dispatch_block_t) // same as calling async(.Main, closure: closure)

async {
    // Asynchronous code

    async_main {
        // Main thread code
    }
}

after()

func after(delay: Double, queue: GCDQueue = .Main, closure: dispatch_block_t)
func delay(delay: Double, queue: GCDQueue = .Main, closure: dispatch_block_t) // Same as above

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

Weak

Container of weak variable

struct Weak<T: AnyObject> {
    weak var value : T?

    init (_ value: T)
}

/// Usage example

let arrayOfViewControllers = [Weak(vc1), Weak(vc2), Weak(vc3)]
let vc1View = arrayOfViewControllers[0].value?.view

Weak Timer

class WeakTimer {
    static func scheduledTimerWithTimeInterval(timeInterval: NSTimeInterval, userInfo: AnyObject? = nil, repeats: Bool = false, callback: () -> Void) -> NSTimer
}

// Usage example

WeakTimer.scheduledTimerWithTimeInterval(1, repeats: true) { [weak self] in
    // Code repeated every 1 second
    // Do something with weak self?
}

Author

Allan Barbato, [email protected]

License

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

Latest podspec

{
    "name": "Swiftility",
    "version": "0.9.2",
    "summary": "allbto utility library to use across my projects.",
    "homepage": "https://github.com/allbto/ios-swiftility",
    "license": {
        "type": "MIT"
    },
    "authors": "Allan Bto",
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/allbto/ios-swiftility.git",
        "tag": "0.9.2"
    },
    "public_header_files": "Swiftility/**/*.h",
    "source_files": "Swiftility/Sources/**/*.{h,m,swift}",
    "requires_arc": true
}

Pin It on Pinterest

Share This