From the course: iOS App Development: RESTful Web Services

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

CodingKeys for naming of properties

CodingKeys for naming of properties - iOS Tutorial

From the course: iOS App Development: RESTful Web Services

Start my 1-month free trial

CodingKeys for naming of properties

- We've seen how using the codable protocol will allow you to create instances of your object based on JSON data and you don't even have to implement the init function and the decodable protocol if your property names and the types from the JSON match up. Of course there'll be times when the property names don't match up and we need to be able to handle that as well. Fortunately there's a way to do that that makes it pretty simple. You use the CodingKeys enumeration. When you define the CodingKeys enumeration you have to provide a definition of each type for all of the names that you're concerned with for your class. If you leave any out they won't be populated in your instance. The CodingKeys enumeration has an underlying type of string and it also declares to implement the protocol of CodingKey. Fortunately you don't have to implement any functions for that, you can use the default implementation. You have to have a case for each property name in your object, with the value of the…

Contents