Menu

1 / 2

Stop using Codable everywhere.

DEV Community·Mok5ha·6 months ago
#Xs1olNbC
#swift#ios#codable#networking#decodable
Reading 0:00
15s threshold

You’ve been over-engineering your Swift models. “Should this model be Decodable or Codable? Am I doing this wrong?” — Here's how to pick the right one.

Swift doesn’t provide a single “JSON protocol.” It gives you three, each with a specific responsibility

Most networking code only needs Decodable

When your app fetches data and displays it, data flows one way — from the server into your models. The Decodable conformance is a signal to anyone reading this code: this model is read-only by design. You don’t need Codable here, just in case.

struct Article : Decodable { let id : Int let title : String let publishedAt : Date let author : Author }

Enter fullscreen mode

Exit fullscreen mode

Use Codable when data moves in both directions

Two situations make Codable the right call:

You’re sending data back to a server

A request body going out as JSON, a form payload, a PUT with updated fields — anything where your Swift type needs to become JSON.

struct UpdateProfileRequest : Encodable { let displayName :…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.