Latest 0.1.0
Homepage https://github.com/VIPER-Swift/VIPERS-Converter-Protocol
License MIT
Platforms ios 8.0, requires ARC
Authors

[![CI Status](http://img.shields.io/travis/Jan Bartel/VIPERS-Converter-Protocol.svg?style=flat)](https://travis-ci.org/Jan Bartel/VIPERS-Converter-Protocol)
Version
License
Platform

Converter implementation used by the iOS application framework ‘VIPERS’.

A converter is an object responsible for converting an element of a specific type into an other element of a type. It supports some methods to check its responsibility for a specific output type and a specific input object (e.g. can i convert input x to type y).

The converter pattern is used in many VIPERS classes (e.g. Router, Wireframe)

A general converter implements the ConverterProtocol


public protocol ConverterProtocol{
    func isResponibleForOutputType<T>(_ type:T.Type) -> Bool
    func isResponsible(_ input:Any) throws -> Bool
    func convert(_ input: Any) throws -> Any
}

There is also a dereived protocol for a converter for specific in and output types

public protocol TypedConverterProtocol : ConverterProtocol{

    associatedtype ConverterOutputType
    associatedtype ConverterInputType

    func isResponibleForOutputType<T>(_ type:T.Type) -> Bool

    func isResponsible(_ input:ConverterInputType) throws -> Bool

    func convert(_ input: ConverterInputType) throws -> ConverterOutputType
}

A simple implementation of a TypedConverter is shown below:


class DateToStringConverter : TypedConverterProtocol{

    typealias ConverterOutputType = String
    typealias ConverterInputType = Date

    func isResponsible(_ input:Date) throws -> Bool{
        return true
    }

    func convert(_ input: Date) throws -> String{
        let formatter = DateFormatter()
        return formatter.string(from: input)
    }
}

There is also a implementation (in the VIPERS-Converter Pod) of a ComposedConverter which aggregates different converters and converts an input with the first possible contained converter


let composedConverter = ComposedConverter()

let dateConverter = DateToStringConverter()
composedConverter.addConverter(dateConverter)

//add some other converters here

let dateConvertedToString : String = try composedConverter.convert(Date())
print(dateConvertedToString)

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

VIPERS-Converter-Protocol is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "VIPERS-Converter-Protocol"

Author

Jan Bartel, [email protected]

License

VIPERS-Converter-Protocol is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "VIPERS-Converter-Protocol",
    "version": "0.1.0",
    "summary": "Converter protocols used by the iOS application framework 'VIPERS'",
    "description": "Converter protocols used by the iOS application framework 'VIPERS'.nA converter is an object responsible to convert one element into an other element.",
    "homepage": "https://github.com/VIPER-Swift/VIPERS-Converter-Protocol",
    "license": "MIT",
    "authors": {
        "Jan Bartel": "[email protected]"
    },
    "source": {
        "git": "https://github.com/VIPER-Swift/VIPERS-Converter-Protocol.git",
        "tag": "0.1.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "Pod/Classes/**/*"
}

Pin It on Pinterest

Share This