개발/swift

Swift codable protocol

lemonade 2020. 10. 17. 15:02
반응형

This post is about codable protocol!

Codable was one of the corestone features of Swift 4.0, bringing with it incredibly smooth conversion between Swift data types and JSON . Codable is a type alias for the Encodable and Decodable protocols.
A type that can convert itself into and out of an external representation.

 

For more details, see apple official docs.

 


With codable, you can use alamofire more easily.

First Make your own model with properties.

 


Second, You have to define your own coding key which refers to json key.

 

Json example


Last, you need to write custom initializer and custom encode method for your model.

 


And what!

You can use own model with alamofire easily.

 

 

Also, you can make dictionary easily too.

 

When makes parameters, it is useful.

 

 

Happy Codding :)

 

반응형
그리드형

'개발 > swift' 카테고리의 다른 글

Swift property observer  (0) 2020.10.20
Swift wkwebview message handler  (0) 2020.10.17
Swift How to get range of substring?  (0) 2020.10.17
Swift frame vs bounds  (0) 2020.10.14
How to get app store version in swift?  (0) 2020.10.13