Featured Development Resources iOS Development Tutorials Swift

Tutorial: Converting Swift Any Structs To JSON Quickly Using A Protocol And The Reflection API

NSJSONSerialization allows you to easily convert an NSArray or NSDictionary to JSON, but does not work directly with Swift structs.

Emil Loer has written a nice tutorial about how you can quickly convert a Swift struct to JSON by using a protocol and the Swift Reflections compatible with NSJSONSerialization , and also how you can easily go a step further so that you can easily serialize data types that are not supported by NSJSONSerialization such as NSDate.

You can find the tutorial over on the Codelle website.

A nice guide on how you can use Swift features to very cleanly convert Swift structs to JSON.