Skip to content
Chainable Swift typecasting
Swift Ruby
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Source
Tests/iOS
.gitignore
.swift-version
IDPCastable.podspec
LICENSE
README.md

README.md

IDPCastable

IDPCastable is a chainable casting type used to avoid excessive iflets and guards, whe you are working with multiple unrelated types expected under a single variable.

You can read more about casting and how it helps you simplify the code in blogposts: Type Inference, Chainable Casting

A perfect example is a UITableView presenting different cells without common protocol. Code, in the way it's usually written:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let identifier = (indexPath.row % 2) == 0 ? "GrayCell" : "PinkCell"

    let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
    if let pinkCell = cell as? PinkCell {
        pinkCell.fill(withModel: indexPath.row)
    }

    if let grayCell = cell as? GrayCell {
        grayCell.fill(withModel: "row = \(indexPath.row), column = \(indexPath.section)")
    }

    return cell
}

Code using IDPCastable:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = (indexPath.row % 2) == 0 ? "GrayCell" : "PinkCell"

        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)

        return castable(cell) { (c: PinkCell) in
                c.fill(withModel: indexPath.row)
            }
            .match { (c: GrayCell) in
                c.fill(withModel: "row = \(indexPath.row), column = \(indexPath.section)")
            }
            .extract()
    }

Example

To run the example project, clone the repo, and run pod install from the Tests/iOS directory first.

Requirements

Swift 3.0

Installation

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

pod "IDPCastable"

Author

Oleksa 'trimm' Korin, oleksandr.korin@idapgroup.com

License

IDPCastable is available under the New BSD license. See the LICENSE file for more info.

You can’t perform that action at this time.