Get rid of that hefty _NSCoding_ code..!!!🤗  As we are all aware, to support encoding and decoding of instances in iOS, a class must adopt the _NSCoding_ protocol and implement its methods: 1. **_init(coder:)_** — Returns an object initialized from data in a given unarchiver. 2. **_encode(with:)_** — Encodes the receiver using a given archiver. **Example:** _init(coder:)_ and _encode(with:)_ must contain the code for each property that needs to be encoded or decoded. 😲 Well, it seems that you have to write so much of redundant code 😖 only with the property name changes. **Copy Paste..Copy Paste..!!!😕😴** But why should I do that? 🙇♀️ If the property names are same as the key names and I don’t have any specific requirements, why don’t _NSCoding_ handle everything at its own end? 🤷♀️ Why do I have to write so much code? Noooooo..!!! 😼 OMG..!!! Another problem 🤦♀️. It doesn’t even support _structs and enums_. 👺 And that means I have to make a _class_ every-time I need to serialize the data, even if I don’t have any class-specific requirement. 🤒 Such a waste of time 🤢. Help me..!!!🙏 Well..well..well..Don’t worry. _Apple_ is here to your rescue again. 🤠 #### What’s new in Swift 4? In its _Swift 4_ release, _Apple_ unveiled a brand new way of data encoding and decoding 🤓 by conforming your custom types with some easy to adopt protocols, 1. **_Encodable_** — for encoding 2. **_Decodable_** — for decoding 3. **_Codable_** — for both encoding as well as decoding It provides support for _class, struct and enum_ as well. 👌👌 So, let’s see what’s in it for us. #### Encodable Protocol A type that can encode itself to an external representation. It is used by the types that can be encoded. It contains a single method: _encode(to:) —_ Encodes this value into the given encoder. #### Decodable Protocol A type that can decode itself from an external representation. It is used by the types that can be decoded. It also contains a single method: _init(from:)_ — Creates a new instance by decoding from the given decoder. #### Codable Protocol A type that can convert itself into and out of an external representation. It is used by the type can be both encoded as well as decoded. typealias Codable = Decodable & Encodable It includes the methods declared in both _Encodable_ as well as _Decodable._ You’ll learn more about _encode(to:)_ and _init(from:)_ in the upcoming sections 👽. #### Codable Type To encode and decode a custom type, we need to make it _Codable_. > The simplest way to make a type codable is to declare its properties using types that are already _Codable_. 1. Built-in _Codable_ types — _String, Int, Double, Data, URL_ 2. _Array, Dictionary, Optional_ are _Codable_ if they contain _Codable_ types So, now that we have seen what _Codable_ is, let’s see how to actually use it to encode and decode our own custom types. Here we go..🙂 #### Encoding — JSONEncoder You can use **_JSONEncoder_** to convert your _codable_ _type_ into _Data_. _JSONEncoder’s_ **_encode(\_:)_** method returns a JSON-encoded representation of the _codable type_. Whatttttt…!!! 😱 Just 1 line of code? That’s it? You’re kidding right? There must be something else..some catch..it can’t be..🤯 😂😎 yes this is the only code you need to write to get it working. Isn’t it awesome 👏?. Just 2 simple steps and you are done. No need for that hefty code anymore with so much of overhead. Hi5 ✋.  Like encoding, you can easily get your hands on decoding as well. So what are you waiting for?🤓 Get on with it..🤗 #### **Decoding —** JSONDecoder Just like _JSONEncoder_, there exist **_JSONDecoder_** that can be used to decode your JSON data back into your _codable type_. _JSONDecoder’s_ **_decode(\_:from:)_** method returns a value of the _codable type_ you specify, decoded from a JSON object. That’s it. That’s how you can encode/decode your Codable Type. **Just 2 steps:** 1. Conform your custom type to _Codable protocol._ 2. Use _JSONEncoder/JSONDecoder_ to encode/decode your custom object. #### Choosing Properties to Encode and Decode — CodingKeys There might be some questions ❓❓coming to your mind, 1. What if I want to omit some properties of the _Codable Type_ from the serialization process? 2. How to encode/decode if some of the keys 🔑 included in the serialized data doesn’t match the property names of the _Codable Type_? Well, _Apple_ provided solution for that too — **_enum CodingKeys._** > Codable types can declare a special nested enumeration named CodingKeys that conforms to the CodingKey protocol. When this enumeration is present, its cases serve as the authoritative list of properties that must be included when instances of a codable type are encoded or decoded. Things to note about **_CodingKeys:_** 1. It has a _Raw Type — String_ and conform to _CodingKey_ protocol. 2. The names of the enum cases should **_exactly match_** 💯 the property names of the _Codable Type_. 3. _Answer to 1st Question_ ✅— Omit the properties from _CodingKeys_ if you want to omit them from encoding/decoding process. A property omitted from _CodingKeys_ needs a default value. 4. _Answer to 2nd Question_ ✅ — _Raw Value_ is the thing you need if the property names of _Codable Type_ doesn’t match the keys in the serialized data. Provide alternative keys by specifying _String_ as the raw-value type for the _CodingKeys_ enumeration. The string you use as a raw value for each enumeration case is the key name used during encoding and decoding. **Example:** In the below code snippet, 1. _var format: String = “png”_ is omitted from the _CodingKeys_ and hence is provided a default value. 2. Properties _title_ and _url_ are renamed to _name_ and _link_ using the _raw value_ in _CodingKeys — case title = “name”_ and _case url = “link”_ #### Encode and Decode Manually ✏️ There exist scenarios when the structure of your _Codable Type_ differs from the structure of its encoded form, _for example_: When you encode a _Photo_ object using the above _Photo_ declaration, the _JSON_ you get looks something like: What if you don’t want “_width”_ and “_height”_ nested in “_size”_? 🤔i.e. your _JSON_ should look something like: In that case, you can provide your own custom logic 📝 of _Encodable_ and _Decodable_ to define your own encoding and decoding logic. You need to implement _encode(to:)_ and _init(from:)_ methods of _Encodable_ and _Decodable_ protocols explicitly. Follow the below steps: 1. Update the _enum CodingKeys_ to include _width_ and _height_ keys instead of _size_. 2. Remove the conformance to _Codable_ from _Photo_. 3. Create a _Photo extension_ conforming to _Encodable_ and implement _encode(to:)_ method. 4. Create a _Photo extension_ conforming to _Decodable_ and implement _init(from:)_ method. #### Limitations 🚫 1. For now you cannot conform to _Codable_ in an _extension_ 🙅♀️. It might be available in further releases.  Too many yets in this release 🙈. I don’t know why _Apple_ is in so much hurry..🤔 2\. You must use a **concrete type** to encode and decode.  You can get a nice use-case of this scenario [here](https://stackoverflow.com/questions/46564650/how-to-define-variable-with-type-of-codable/46580238#46580238). ### Promotions Don’t forget to read my other articles: 1. [Drag It & Drop It in Collection & Table — iOS 11](https://medium.com/@p.gpt10/drag-it-drop-it-in-collection-table-ios-11-6bd28795b313) 2. [UITableView Leading & Trailing Swipe Actions in iOS 11](https://hackernoon.com/uitableview-leading-trailing-swipe-actions-in-ios-11-18cb1f267f8a "https://hackernoon.com/uitableview-leading-trailing-swipe-actions-in-ios-11-18cb1f267f8a") 3. [Swift 4.0 Migration — ERROR..!! ERROR..!!](https://hackernoon.com/swift-4-0-migration-error-error-8fbf13c2ec2 "https://hackernoon.com/swift-4-0-migration-error-error-8fbf13c2ec2") 4. [All you need to know about Today Extensions (Widget) in iOS 10](https://hackernoon.com/app-extensions-and-today-extensions-widget-in-ios-10-e2d9fd9957a8 "https://hackernoon.com/app-extensions-and-today-extensions-widget-in-ios-10-e2d9fd9957a8") 5. [UICollectionViewCell selection made easy..!!](https://hackernoon.com/uicollectionviewcell-selection-made-easy-41dae148379d "https://hackernoon.com/uicollectionviewcell-selection-made-easy-41dae148379d") Feel free to leave comments if you have any doubts. 🙂🙃