• Stars
    star
    353
  • Rank 120,322 (Top 3 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 5 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A convenient way to handle default values with Swift Codable types

DefaultCodable

Swift 5.1 Swift Package Manager Twitter: @gonzalezreal

DefaultCodable is a Swift ยตpackage that provides a convenient way to define default values in Codable types for properties that are not present or have a nil value.

Usage

Consider a hypothetical model for Apple products., in which only the property name is required.

enum ProductType: String, Codable, CaseIterable {
  case phone, pad, mac, accesory
}

struct Product: Codable {
  var name: String
  var description: String?
  var isAvailable: Bool?
  var type: ProductType?
}

Using the @Default property wrapper, we can provide default values for the properties not required and thus get rid of the optionals in our model.

struct Product: Codable {
  var name: String
  
  @Default<Empty>
  var description: String
  
  @Default<True>
  var isAvailable: Bool
  
  @Default<FirstCase>
  var type: ProductType
}

With that in place, we can safely decode the following JSON into a Product type.

{
  "name": "iPhone 11 Pro"
}

The resulting Product instance is using the default values for those properties not present in the JSON.

โ–ฟ Product
- name : "iPhone 11 Pro"
- description : ""
- isAvailable : true
- type : ProductType.phone

If you encode the result back, the resulting JSON will be the same as the one we started with. The @Default property wrapper will not encode the value if it is equal to the default value.

The @Default property wrapper takes a DefaultValueProvider as a parameter. This type provides the default value when a value is not present or is nil.

protocol DefaultValueProvider {
  associatedtype Value: Equatable & Codable
  
  static var `default`: Value { get }
}

DefaultCodable provides the following implementations for your convenience:

Empty

It provides an empty instance of a String, Array or any type that implements RangeReplaceableCollection.

EmptyDictionary

It provides an empty instance of a Dictionary.

True and False

Provide true and false respectively for Bool properties.

Zero and One

Provide 0 and 1 respectively for Int properties.

FirstCase

It provides the first case of an enum type as the default value. The enum must implement the CaseIterable protocol.

ZeroDouble

Provide 0 for Double properties.

Default values for custom types

Your custom type must implement the DefaultValueProvider protocol to be compatible with the @Default property wrapper.

Consider the following type that models a role in a conversation:

struct Role: Codable, Equatable, Hashable, RawRepresentable {
  let rawValue: String

  init?(rawValue: String) {
    self.rawValue = rawValue
  }

  static let user = Role(rawValue: "user")!
  static let bot = Role(rawValue: "bot")!
}

If we want the default role to be user, we can implement DefaultValueProvider as follows:

extension Role: DefaultValueProvider {
  static let `default` = user
}

With that in place, we can use the @Default property wrapper in any type that has a property of type Role:

struct ChannelAccount: Codable {
  var name: String
  
  @Default<Role>
  var role: Role
}

Installation

Using the Swift Package Manager

Add DefaultCodable as a dependency to your Package.swift file. For more information, see the Swift Package Manager documentation.

.package(url: "https://github.com/gonzalezreal/DefaultCodable", from: "1.0.0")

Help & Feedback

  • Open an issue if you need help, if you found a bug, or if you want to discuss a feature request.
  • Open a PR if you want to make some change to DefaultCodable.
  • Contact @gonzalezreal on Twitter.

More Repositories

1

swift-markdown-ui

Display and customize Markdown text in SwiftUI
Swift
2,372
star
2

Groot

From JSON to Core Data and back.
Objective-C
534
star
3

Vertigo

Simple full screen image viewer with image zoom custom view controller transition
Objective-C
355
star
4

AttributedText

Render attributed strings in SwiftUI
Swift
122
star
5

Markup

Lightweight markup text formatting in Swift
Swift
95
star
6

AdaptiveCardUI

Snippets of UI, authored in JSON and rendered with SwiftUI
Swift
92
star
7

ReadingList

An example on using the Mantle Modeling Framework with Overcoat AFNetworking extension.
Objective-C
86
star
8

NetworkImage

Asynchronous image loading in SwiftUI
Swift
69
star
9

SimpleNetworking

Scalable and composable API Clients using Swift Combine
Swift
51
star
10

IndeterminateTypesWithCodable

Indeterminate Types with Codable in Swift
Swift
37
star
11

Borders

Sample code for Consuming Web Services with Swift and ReactiveX
Swift
33
star
12

TGRDataSource

Convenience UITableView and UICollectionView data sources
Objective-C
24
star
13

SwiftCommonMark

Parse and create CommonMark documents in Swift.
C
23
star
14

Gig

A great Twitter API client for Objective-C
Objective-C
19
star
15

Reusable

iOS cell registration and reusing with generics and protocol extensions
Swift
13
star
16

DirectLine

Swift client library for Microsoft Bot Framework's Direct Line protocol
Swift
11
star
17

rxswift-gentle-introduction

A gentle introduction to RxSwift http://www.vsmobile.tech
7
star
18

swift-any-value

A Swift Codable type that serves as a placeholder for any JSON value
Swift
3
star
19

NSSpain2016

From Design to Code
3
star
20

TwitterTimeline

Source code for my article 'Implementando una timeline de Twitter con Core Data'
Objective-C
2
star