NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

Extension to add protocol conformance

Forums > Swift

I ran across some code that adds an extension to this struct having the Identifiable protocol.

Why not just do this instead?

struct SomeItem: Decodable, Identifiable {
import Foundation

struct SomeItem: Decodable {
    let id: Int
    let url: URL

    private enum CodingKeys: String, CodingKey {
        case id
        case url = "webURL"

extension SomeItem: Identifiable { }


You could do that.

A reason not to is code organization. Keeping all the stuff for conforming to a particular protocol in its own extension makes things just a little bit neater.


Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.

Try the book!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.