Skip to content

idapgroup/IDPTypes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Swift types and type extensions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published