Skip to content
Type-erased wrappers for Encodable, Decodable, and Codable values
Branch: master
Clone or download
Latest commit e38c4bb Jan 31, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
AnyCodable.playground
AnyCodable.xcodeproj Add explicit, shared AnyCodable scheme Jan 2, 2019
AnyCodable.xcworkspace
Sources/AnyCodable Initial commit Apr 23, 2018
Tests
.gitignore
.travis.yml Add .travis.yml Jan 2, 2019
LICENSE.md
Package.swift Initial commit Apr 23, 2018
README.md Update README.md Jan 31, 2019

README.md

AnyCodable

Build Status

Type-erased wrappers for Encodable, Decodable, and Codable values.

This functionality is discussed in Chapter 3 of Flight School Guide to Swift Codable.

AnyEncodable

import AnyCodable

let dictionary: [String: AnyEncodable] = [
    "boolean": true,
    "integer": 1,
    "double": 3.14159265358979323846,
    "string": "string",
    "array": [1, 2, 3],
    "nested": [
        "a": "alpha",
        "b": "bravo",
        "c": "charlie"
    ]
]

let encoder = JSONEncoder()
let json = try! encoder.encode(dictionary)

AnyDecodable

let json = """
     {
         "boolean": true,
         "integer": 1,
         "double": 3.14159265358979323846,
         "string": "string",
         "array": [1, 2, 3],
         "nested": {
             "a": "alpha",
             "b": "bravo",
             "c": "charlie"
         }
     }
""".data(using: .utf8)!

let decoder = JSONDecoder()
let dictionary = try! decoder.decode([String: AnyDecodable].self, from: json)

AnyCodable

AnyCodable can be used to wrap values for encoding and decoding.

License

MIT

Contact

Mattt (@mattt)

You can’t perform that action at this time.