Latest 0.1.3
Homepage https://github.com/idapgroup/IDPCastable
License New BSD
Platforms ios 8.0, osx 10.9, tvos 9.0, requires ARC
Authors

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, [email protected]

License

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

Latest podspec

{
    "name": "IDPCastable",
    "version": "0.1.3",
    "summary": "Chainable Swift typecasting",
    "description": "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.",
    "homepage": "https://github.com/idapgroup/IDPCastable",
    "license": {
        "type": "New BSD",
        "file": "LICENSE"
    },
    "authors": {
        "IDAP Group": "[email protected]"
    },
    "source": {
        "git": "https://github.com/idapgroup/IDPCastable.git",
        "tag": "0.1.3"
    },
    "requires_arc": true,
    "platforms": {
        "ios": "8.0",
        "osx": "10.9",
        "tvos": "9.0"
    },
    "header_mappings_dir": "Source",
    "source_files": "Source/**/*.{swift,h,m,c,cpp}",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This