Swiftpack.co - to4iki/ForcibleValue as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by to4iki.
to4iki/ForcibleValue 0.1.0
Better Codable through Property Wrappers.
⭐️ 0
🕓 9 weeks ago
.package(url: "https://github.com/to4iki/ForcibleValue.git", from: "0.1.0")

ForcibleValue

Swift 5 CocoaPods compatible Carthage compatible SPM compatible MIT License

Decode value that is sometimes an Int and other times a String your Codable structs through property wrappers.

Installation

Swift Package Manager

.package(url: "https://github.com/to4iki/ForcibleValue", from: "0.1.0")

Cocoapods

pod 'ForcibleValue'

Carthage

github "to4iki/ForcibleValue"

Usage

You can define a variable of forcible type to decode your structs.

struct User: Decodable {
    @ForcibleString var name: String
    @ForcibleInt var age: Int
    @ForcibleDouble var height: Double
    @ForcibleFloat var weight: Float
    @ForcibleBool var isAdmin: Bool
}

let json = """
{
    "name": 1234,
    "age": "30",
    "height": "172.3",
    "weight": "60.0",
    "isAdmin": 1
}
""".data(using: .utf8)

do {
    let user = try JSONDecoder().decode(User.self, from: json!)
    print(user) // User(_name: 1234, _age: 30, _height: 172.3, _weight: 60.0, _isAdmin: true)
} catch {
    print(error)
}

Examples

License

ForcibleValue is released under the MIT license.

GitHub

link
Stars: 0
Last commit: 3 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics