A protocol that allows any class to be printed as if it were a struct or a JSON object.

Overview

Carthage compatible Platform

ReflectedStringConvertible

A protocol that extends CustomStringConvertible and uses reflection to add a detailed textual representation to any class. Two styles are supported:

  1. normal: Similar to Swift's default textual representation of structs.
  2. json: Pretty JSON representation.

Installation

Cocoapods

Add the following to your Podfile:

pod 'ReflectedStringConvertible'

Carthage

Add the following to your Cartfile:

github "mattcomi/ReflectedStringConvertible"

Usage

Simply import ReflectedStringConvertible and conform to the ReflectedStringConvertible protocol:

import ReflectedStringConvertible

class YourClass: ReflectedStringConvertible {
  // that's all.
}

For example:

class Person: ReflectedStringConvertible {
  var name: String
  var age: Int

  init(name: String, age: Int) {
    self.name = name
    self.age = age
  }
}

print(Person(name: "Matt", age: 33)) outputs:

Person(name: "Matt", age: 33)

A style may be specified with reflectedDescription(style:). The default style is normal. That is, calling description is the same as calling reflectedDescription(.normal).

For example, print(Person(name: "Matt", age: 33).reflectedDescription(.json)) outputs:

{
  "age" : 33,
  "name" : "Matt"
}

Refer to the API Documentation for further information.

Features

ReflectedStringConvertible stored properties

ReflectedStringConvertible objects with ReflectedStringConvertible stored properties are handled correctly:

class Movie: ReflectedStringConvertible {
  var title: String
  var year: Int

  // another ReflectedStringConvertible
  var director: Person

  init(title: String, year: Int, director: Person) {
    self.title = title
    self.year = year
    self.director = director
  }
}

let george = Person(name: "George Miller", age: 71)
let movie = Movie(title: "Mad Max", year: 2015, director: george)

print(movie.reflectedDescription(.normal)) (or just print(movie)) outputs:

Movie(title: "Mad Max", year: 2015, director: Person(name: "George Miller", age: 71))

And print(movie.reflectedDescription(.json)) outputs:

{
  "title" : "Mad Max",
  "year" : 2015,
  "director" : {
    "age" : 71,
    "name" : "George Miller"
  }
}

Collections

ReflectedStringConvertible objects within Array, Dictionary and Set collections are handled correctly:

class Series: ReflectedStringConvertible {
  var title: String
  var cast: [Person]

  init(title: String, cast: [Person]) {
    self.cast = cast
  }
}

var cast = [Person]()

cast.append(Person(name: "Justin Theroux", age: 44))
cast.append(Person(name: "Carrie Coon", age: 35))

let series = Series(title: "The Leftovers", cast: cast)

print(series) outputs:

TVShow(title: "The Leftovers", cast: [Person(name: "Justin Theroux", age: 44), Person(name: "Carrie Coon", age: 35)])

print(series.reflectedDescription(.json)) outputs:

{
  "title" : "The Leftovers",
  "cast" : [
    {
      "age" : 44,
      "name" : "Justin Theroux"
    },
    {
      "age" : 35,
      "name" : "Carrie Coon"
    }
  ]
}

Credits

Developed by Matt Comi (@mattcomi)

You might also like...
URLEmbeddedView automatically caches the object that is confirmed the Open Graph Protocol.
URLEmbeddedView automatically caches the object that is confirmed the Open Graph Protocol.

URLEmbeddedView Features Simple interface for fetching Open Graph Data Be able to display Open Graph Data Automatically caching Open Graph Data Automa

URLEmbeddedView automatically caches the object that is confirmed the Open Graph Protocol.
URLEmbeddedView automatically caches the object that is confirmed the Open Graph Protocol.

URLEmbeddedView Features Simple interface for fetching Open Graph Data Be able to display Open Graph Data Automatically caching Open Graph Data Automa

🎧 Protocol driven object observation

Listenable Swift object that provides an observable platform for multiple listeners. Requirements iOS 9.0+ Xcode 9.x+ Swift 4 Installation Listenable

Convert your own struct/enum to AnyObject easily.

AnyObjectConvertible Convert your own struct/enum to AnyObject easily. Sumally We can cast everything as AnyObject at Swift3.0 🎉 So, we would not nee

Monaka convert custom struct to NSData.
Monaka convert custom struct to NSData.

Monaka Overview Monaka convert custom struct and fundamental values to NSData (also nested array and dictionary). Purpose You can persistent store of

A Swift extension that adds toast notifications to the UIView object class.
A Swift extension that adds toast notifications to the UIView object class.

Toast-Swift Toast-Swift is a Swift extension that adds toast notifications to the UIView object class. It is intended to be simple, lightweight, and e

HexColor is a simple extension that lets you initialize UIColors the way they were meant to be initialized: With hex integer values

HexColor is a simple extension that lets you initialize UIColors the way they were meant to be initialized: With hex integer values. Requirem

How Swift standard types and classes were supposed to work.
How Swift standard types and classes were supposed to work.

How Swift standard types and classes were supposed to work. A collection of useful extensions for the Swift Standard Library, Foundation, and UIKit.

Android/iOS Apps created to practice with different iOS/Android Tech. These apps were built to have similar feature sets using native Android/iOS.

AgilityFitTodayApp Android/iOS Apps created to practice with different iOS/Android Tech. These apps were built to have similar feature sets using nati

Codeless drop-in universal library allows to prevent issues of keyboard sliding up and cover UITextField/UITextView. Neither need to write any code nor any setup required and much more.
Codeless drop-in universal library allows to prevent issues of keyboard sliding up and cover UITextField/UITextView. Neither need to write any code nor any setup required and much more.

IQKeyboardManager While developing iOS apps, we often run into issues where the iPhone keyboard slides up and covers the UITextField/UITextView. IQKey

Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift

EVReflection General information At this moment the master branch is tested with Swift 4.2 and 5.0 beta If you want to continue using EVReflection in

Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift

EVReflection General information At this moment the master branch is tested with Swift 4.2 and 5.0 beta If you want to continue using EVReflection in

JSON object with Swift

JSON JSON using @dynamicMemberLookup, which allows us to write more natural code

Framework for easily parsing your JSON data directly to Swift object.
Framework for easily parsing your JSON data directly to Swift object.

Server sends the all JSON data in black and white format i.e. its all strings & we make hard efforts to typecast them into their respective datatypes

Simple JSON Object mapping written in Swift

ObjectMapper ObjectMapper is a framework written in Swift that makes it easy for you to convert your model objects (classes and structs) to and from J

AppCodableStorage - Extends `@AppStorage` in SwiftUI to support any Codable object

AppCodableStorage Extends @AppStorage in SwiftUI to support any Codable object.

Xcode Plugin helps you find missing methods in your class header, protocols, and super class, also makes fast inserting.

FastStub-Xcode Life is short, why waste it on meaningless typing? What is it? A code generating feature borrowed from Android Studio. FastStub automat

JSONHelper - ✌ Convert anything into anything in one operation; JSON data into class instances, hex strings into UIColor/NSColor, y/n strings to booleans, arrays and dictionaries of these; anything you can make sense of!

JSONHelper Convert anything into anything in one operation; hex strings into UIColor/NSColor, JSON strings into class instances, y/n strings to boolea

JSPatch bridge Objective-C and Javascript using the Objective-C runtime. You can call any Objective-C class and method in JavaScript by just including a small engine. JSPatch is generally used to hotfix iOS App.

JSPatch 中文介绍 | 文档 | JSPatch平台 请大家不要自行接入 JSPatch,统一接入 JSPatch 平台,让热修复在一个安全和可控的环境下使用。原因详见 这里 JSPatch bridges Objective-C and JavaScript using the Object

Comments
Releases(1.3.1)
Owner
Matt Comi
Matt Comi
Swift library for working with Debug Adapter Protocol (DAP)

DebugAdapterProtocol This is a Swift library for interacting with Debug Adapter Protocol. It contains type definitions and utilities useful for both s

Chime 2 Apr 12, 2022
Class for logging excessive blocking on the main thread

Watchdog Class for logging excessive blocking on the main thread. It watches the main thread and checks if it doesn’t get blocked for more than define

Wojtek Lukaszuk 1.8k Dec 6, 2022
A simple class that wraps the process of saving or loading a struct or class into a single file

EZFile This is a simple class that wraps the process of saving or loading a stru

null 7 May 16, 2022
A fast, convenient and nonintrusive conversion framework between JSON and model. Your model class doesn't need to extend any base class. You don't need to modify any model file.

MJExtension A fast, convenient and nonintrusive conversion framework between JSON and model. 转换速度快、使用简单方便的字典转模型框架 ?? ✍??Release Notes: more details Co

M了个J 8.5k Jan 3, 2023
RandMyMod base on your own struct or class create one or a set of instance, which the variable's value in the instance is automatic randomized.

RandMyMod is an IOS Native Framework helps you generate one or a set of variable base on your own model. No matter your model is Class / Struct. Insta

郭介騵 17 Sep 24, 2022
A type-safe, protocol-based, pure Swift database offering effortless persistence of any object

There are many libraries out there that aims to help developers easily create and use SQLite databases. Unfortunately developers still have to get bogged down in simple tasks such as writing table definitions and SQL queries. SwiftyDB automatically handles everything you don't want to spend your time doing.

Øyvind Grimnes 489 Sep 9, 2022
Scanner for decks of cards with bar codes printed on card edges

The Nettle Magic Project This deck of cards has a bar code printed on the edge of each card. Scanning these bar codes would reveal where every card is

Paul Nettle 793 Dec 14, 2022