Tip on Swift & Codable

Tip on Swift & Codable

When you're working with swift and consuming web api thanks to Codable you can now automatically map web  JSON snakecase keys  to swifts camelcase just by using :

var decoder = JSONDecoder() 
decoder.keyDecodingStrategy = .convertFromSnakeCase

and it will turn mobile_development to mobileDevelopment

Although its nice and cool if you consume your own API. If you're relying on external it is safer to create private enum where and map your keys explicitly like so:

extension MobileDevelopment {
	private enum CodingKeys:String,CodingKey{
            case iOS
            case android
            case progressiveWebApps = "progressive_web_apps"

Have a nice day!

Show Comments