Updated 31 May 2021
Codable is a type alias for the Encodable and Decodable protocols. According to Apple documentation “A type that can convert itself into and out of an external representation”.
Encodable Protocols:- The process in which custom type is converted into other representation is known as encoding or serialization. And to do so encodable protocols have to conform.
Decodable Protocols:- The process in which others’ representation is converted into your custom type is known as decoding or deserialization. And to do so decodable protocols have to conform.
And to support both encoding and decoding Codable protocols have to conform.
Let see an example.
1 2 3 4 |
struct Employee: Codable{ var empId: Int? var empname: String? } |
Now Employee type can be encoded and decoded both.
It is used to encode our type to another type. Like converting into data.
Let see an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
struct Employee: Codable{ var empId: Int? var empname: String? var employeeDetails: EmployeeDetails? } struct EmployeeDetails: Codable{ var designation: String? var salary: Double? var age: Int? var mobile: Int? } |
Now, we will encode Employee into data.
1 2 3 4 5 6 7 8 |
let empDetails = EmployeeDetails(designation: "eng", salary: 400000.00, age: 25, mobile: 512122121) let employee = Employee(empId: 101, empname: "test", employeeDetails: empDetails) do{ let data = try JSONEncoder().encode(employee) print(data) }catch let err{ print(err.localizedDescription) } |
when we print the data it prints in Byte.
Now, we will convert the data again into Employee type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
var empInData = Data() let empDetails = EmployeeDetails(designation: "eng", salary: 400000.00, age: 25, mobile: 512122121) let employee = Employee(empId: 101, empname: "test", employeeDetails: empDetails) //Encoding do{ let data = try JSONEncoder().encode(employee) empInData = data print(data) }catch let err{ print(err.localizedDescription) } //Decoding do{ let decodedData = try JSONDecoder().decode(Employee.self, from: empInData ) print(decodedData) } catch let err{ print(err.localizedDescription) } |
Below is the output of decoding.
1 |
Employee(empId: Optional(101), empname: Optional("test"), employeeDetails: Optional(Garma.EmployeeDetails(designation: Optional("eng"), salary: Optional(400000.0), age: Optional(25), mobile: Optional(512122121)))) |
If you only want to Encode then you have to conform to Encodable protocol and vice versa.
If you want to use a different key name in your custom type than the actual JSON key. In that case, we will use CodingKeys enum. And will conforms CodingKey protocol.
Let see an example of it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
struct EmployeeDetails: Codable{ var designation: String? var salary: Double? var age: Int? var mobile: Int? enum CodingKeys: String, CodingKey { case designation case salary = "emp_salary" case age = "emp_age" case mobile } } |
You can also check other blogs from here. if you have any issue or suggestion you can leave your query/suggestion in the comment section.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.