From the course: iOS App Development: RESTful Web Services

Unlock the full course today

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

Codable for arrays

Codable for arrays - iOS Tutorial

From the course: iOS App Development: RESTful Web Services

Start my 1-month free trial

Codable for arrays

- [Instructor] So far we've primarily looked at how we can handle a single instance being passed back from the server and use Codable to create an instance of our class. But in many cases, we'll want to fetch multiple instances from the server. For instance, if you're searching for certain items or even downloading all of the instances of a certain class from the database. So we can handle that with arrays and still use Codable. We can use the same encoder and even the same call. However, we have to change the type that we pass in as what we expect to be parsed. In our case we can simply surround our music with the square brackets to indicate that we're expecting an array back. This works because the decode call on JSONDecoder accepts the type as a generic and specifies that as the return type. And we'll look at that in the code. If we use command-control-click on the decode call on line 47, we can see that the definition specifies using a generic as the type being passed in and also…

Contents