Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Encodable, Decodable Vs Codable

Found in Apple doc, that Codable protocol is composed of Encodable and Decodable. Thus,

Codable = Encodable & Decodable

Now, let's say I implemented below classes,

class X: Codable {
    var name: String
}

class Y: Encodable, Decodable {
    var name: String
}

class Z: Encodable & Decodable {
    var name: String
}

So, is there any functional difference among the classes, X, Y and Z?

If there is no deference why can't we use & in the places of ,?

like image 252
Sazzad Hissain Khan Avatar asked Mar 20 '26 14:03

Sazzad Hissain Khan


1 Answers

No there isn't any difference , Codable is a typealias for Encodable & Decodable , so it combines the 2 protocols you're free to use any way

In Swift & is the same as , in protocol composition so Encodable, Decodable = Encodable & Decodable = Codable

like image 92
Sh_Khan Avatar answered Mar 23 '26 04:03

Sh_Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!